Python Tensorflow and Tflearn Execptions - python

I am trying to make a chatbot from website : https://hashdork.com/create-a-deep-learning-chatbot-with-python/
however the code is not working for some reason. It is maybe because of my formatting python.
I have tried to add a model.tflearn file, not working.
Had to remove data.pickle for every single run.
I have no idea what the exception is told me to do.
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tflearn
import tensorflow
import random
import json
import pickle
with open("intents.json") as file:
data = json.load(file)
try:
with open("data.pickle", "rb") as f:
words, labels, training, output = pickle.load(f)
except:
words = []
labels = []
docs_x = []
docs_y = []
for intent in data["intents"]:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_x.append(wrds)
docs_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
words = [stemmer.stem(w.lower()) for w in words if w != "?"]
words = sorted(list(set(words)))
labels = sorted(labels)
training = []
output = []
out_empty = [0 for _ in range(len(labels))]
for x, doc in enumerate(docs_x):
bag = []
wrds = [stemmer.stem(w.lower()) for w in doc]
for w in words:
if w in wrds:
bag.append(1)
else:
bag.append(0)
output_row = out_empty[:]
output_row[labels.index(docs_y[x])] = 1
training.append(bag)
output.append(output_row)
training = numpy.array(training)
output = numpy.array(output)
with open("data.pickle", "wb") as f:
pickle.dump((words, labels, training, output), f)
tensorflow.compat.v1.reset_default_graph()
net = tflearn.input_data(shape=[None, len(training[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=100, batch_size=8, show_metric=True)
model.save("model.tflearn")
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i, w in enumerate(words):
if w == se:
bag[i] = 1
return numpy.array(bag)
def chat():
print("Start talking with the bot (type quit to stop)!")
while True:
inp = input("You: ")
if inp.lower() == "quit":
break
results = model.predict([bag_of_words(inp, words)])
results_index = numpy.argmax(results)
tag = labels[results_index]
for tg in data["intents"]:
if tg['tag'] == tag:
responses = tg['responses']
print(random.choice(responses))
chat()
Terminal:
tflearn.is_training(True, session=self.session) File "/usr/local/lib/python3.7/site-packages/tflearn/config.py", line 95, in is_training tf.get_collection('is_training_ops')[0].eval(session=session) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 913, in eval return _eval_using_default_session(self, feed_dict, self.graph, session) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5512, in _eval_using_default_session return session.run(tensors, feed_dict) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 958, in run run_metadata_ptr) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1104, in _run raise RuntimeError('Attempted to use a closed Session.') RuntimeError: Attempted to use a closed Session.
Any thoughts?

Related

Getting a RuntimeError while running chatbot using Pytorch and NLTK library (python engineer tutorial code)

I have written this line "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')" still getting the following errror. I have provided my all code that i written till now please with this.
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat1 in method wrapper_addmm)
Train.py File
import json
from nltk_utils import tokenize, stem, bag_of_words
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from model import NeuralNet
with open('intents.json', 'r') as f:
intents = json.load(f)
all_words = []
tags = []
xy = []
for intent in intents['intents']:
tag = intent['tag']
tags.append(tag)
for pattern in intent['patterns']:
w = tokenize(pattern)
all_words.extend(w)
xy.append((w, tag))
ignore_words = ['?', '!', '.', ',']
all_words = [stem(w) for w in all_words if w not in ignore_words]
all_words = sorted(set(all_words))
tags = sorted(set(tags))
x_train = []
y_train = []
for (pattern_sentence, tag) in xy:
bag = bag_of_words(pattern_sentence, all_words)
x_train.append(bag)
label = tags.index(tag)
y_train.append(label) # CrossEntropyLoss
x_train = np.array(x_train)
y_train = np.array(y_train)
class ChatDataset(Dataset):
def __init__(self):
self.n_samples = len(x_train)
self.x_data = x_train
self.y_data = y_train
# dataset[idx]
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.n_samples
batch_size = 8
hidden_size = 8
output_size = len(tags)
input_size = len(x_train[0])
learning_rate = 0.001
num_epochs = 1000
print(input_size, len(all_words))
print(output_size, tags)
dataset = ChatDataset()
train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = NeuralNet(input_size, hidden_size, output_size).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for(words, labels) in train_loader:
words = words.to(device)
labels = labels.type(torch.LongTensor) # <----- Here (Casting)
labels = labels.to(device)
# forward
outputs = model(words)
loss = criterion(outputs, labels)
# backward and optimizer step
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 100 == 0:
print(f'epoch {epoch + 1}/{num_epochs}, loss={loss.item():.4f}')
print(f'final loss: {loss.item():.4f}')
data = {
"model_state" : model.state_dict(),
"input_size" : input_size,
"output_size" : output_size,
"hidden_size" : hidden_size,
"all_words" : all_words,
"tags" : tags,
}
FILE = "data.pth"
torch.save(data, FILE)
print(f'training complete , file saved to {FILE}')
chat.py file
import random
import json
import torch
from model import NeuralNet
from nltk_utils import bag_of_words, tokenize
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cuda')
with open('intents.json', 'r') as f:
intents = json.load(f)
FILE = 'data.pth'
data = torch.load(FILE)
input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data["all_words"]
tags = data["tags"]
model_state = data["model_state"]
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
bot_name = 'Sam'
print("Let's chat: type 'quit' to exit")
while True:
sentence = input('You: ')
if sentence == "Quit":
break
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents["intents"]:
if tag == intent["tag"]:
print(f"{bot_name}: {random.choice(intent['responses'])}")
else:
print(f"{bot_name}: I do not understand...:(")
After running the chat.py. while chatbot is trying to send their response then it giving the runtime error.
I should get the response from chatbot. but getting the following error
Error Image

ValueError in lables

Error:-
label = tags.index(tag)
ValueError: 'greeting' is not in list
My Code:-
import numpy as np
import json
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from NeuralNetwork import bag_of_words, tokenize, stem
from brain import NeuralNet
import nltk
nltk.download('punkt')
with open('intents.json', 'r') as f:
intents = json.load(f)
all_words = []
tags = []
xy = []
for intent in intents['intents']:
tag = intent['tag']
tags.append(tag)
for pattern in intent['patterns']:
print(pattern)
w = tokenize(pattern)
all_words.extend(w)
xy.append((w, tag))
ignore_words = [',','?','!','/','.']
all_words = [stem(w) for w in all_words if w not in ignore_words]
all_words = sorted(set(all_words))
tags = sorted(set(tag))
x_train = []
y_train = []
for (pattern_sentence,tag) in xy:
bag = bag_of_words(pattern_sentence, all_words)
x_train.append(bag)
label = tags.index(tag)
y_train.append(label)
x_train = np.array(x_train)
y_train = np.array(y_train)
num_epochs = 1000
batch_size = 8
learning_rate = 0.001
input_size = len(x_train[0])
hidden_size = 8
output_size = len(tags)
print("Training the Model...")
class ChatDataset(Dataset):
def __init__(self):
self.n_samples = len(x_train)
self.x_data = x_train
self.y_data = y_train
def __getitem__(self, index):
return self.x_data[index],self.y_data[index]
def __len__(self):
return self.n_samples
dataset = ChatDataset()
train_loader = DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True,
num_workers=0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = NeuralNet(input_size, hidden_size,output_size).to(device=device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)
for epoch in range(num_epochs):
for (words,labels) in train_loader:
words = words.to(device)
labels = labels.to(dtype = torch.long).to(device)
outputs = model(words)
loss = criterion(outputs,labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
print(f'Final Loss: {loss.item():.4f}')
data = {
"model_state":model.state_dict(),
"input_size":input_size,
"hidden_size":hidden_size,
"output_size":output_size,
"all_words":all_words,
"tags":tags
}
FILE = "TrainData.pth"
torch.save(data,FILE)
print(f"Training Complete, File Saved to {FILE}")
I have been trying to solve it, but I can not come up with a solution.
Can you guys please help me out?
It is having error at line 40
qeht efgr get ugergeg
iuh9oiuhjruiwhgreuhgiuqhoeiugrhiuehroiuhqiurhtieruhiurhiuhtiurehyiuhriuhqiuhry

Use Keras Neural Translation Model (h5 format model) in spacy v3

first 'im not a developer by trade, my developer is not available for health reasons but i have some experience in python/spacy development. I need some guidance in this problem:
I have a h5 keras model which was written to translate some dead language from its latin alphabet written form into arabic "letters" using neural machine translation.
here's the code for creating and training the keras model (dataset.txt is some tab delimited sentences, one in latin alphabet and the other one in arabic):
import string
import re
from unicodedata import normalize
from numpy import array
from pickle import load
from pickle import dump
from numpy.random import rand
from numpy.random import shuffle
from numpy import argmax
import tensorflow as tf
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Embedding
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
# load doc into memory
def load_doc(filename):
# open the file as read only
file = open(filename, mode='rt', encoding='utf-8')
# read all text
text = file.read()
# close the file
file.close()
return text
# split a loaded document into sentences
def to_pairs(doc):
lines = doc.strip().split('\n')
pairs = [line.split('\t') for line in lines]
return pairs
# clean a list of lines
def clean_pairs(lines):
cleaned = list()
for pair in lines:
clean_pair = list()
for line in pair:
clean_pair.append(' '.join(line))
cleaned.append(clean_pair)
return array(cleaned)
# save a list of clean sentences to file
def save_clean_data(sentences, filename):
dump(sentences, open(filename, 'wb'))
print('Saved: %s' % filename)
# load dataset
filename = 'dataset.txt'
doc = load_doc(filename)
# split into latin-bok pairs
pairs = to_pairs(doc)
# clean sentences
clean_pairs = clean_pairs(pairs)
# save clean pairs to file
save_clean_data(clean_pairs, 'latin-bok.pkl')
# spot check
for i in range(100):
print('[%s] => [%s]' % (clean_pairs[i,0], clean_pairs[i,1]))
# load a clean dataset
def load_clean_sentences(filename):
return load(open(filename, 'rb'))
# save a list of clean sentences to file
def save_clean_data(sentences, filename):
dump(sentences, open(filename, 'wb'))
print('Saved: %s' % filename)
# load dataset
raw_dataset = load_clean_sentences('latin-bok.pkl')
# reduce dataset size
n_sentences = 10000
dataset = raw_dataset[:, :]
# random shuffle
shuffle(dataset)
# split into train/test
train, test = dataset[:70000], dataset[70000:]
# save
save_clean_data(dataset, 'latin-bok-both.pkl')
save_clean_data(train, 'latin-bok-train.pkl')
save_clean_data(test, 'latin-bok-test.pkl')
# load a clean dataset
def load_clean_sentences(filename):
return load(open(filename, 'rb'))
# fit a tokenizer
def create_tokenizer(lines):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(lines)
return tokenizer
# max sentence length
def max_length(lines):
return max(len(line.split()) for line in lines)
# encode and pad sequences
def encode_sequences(tokenizer, length, lines):
# integer encode sequences
X = tokenizer.texts_to_sequences(lines)
# pad sequences with 0 values
X = pad_sequences(X, maxlen=length, padding='post')
return X
# one hot encode target sequence
def encode_output(sequences, vocab_size):
ylist = list()
for sequence in sequences:
encoded = to_categorical(sequence, num_classes=vocab_size)
ylist.append(encoded)
y = array(ylist)
y = y.reshape(sequences.shape[0], sequences.shape[1], vocab_size)
return y
# define NMT model
def define_model(src_vocab, tar_vocab, src_timesteps, tar_timesteps, n_units):
model = Sequential()
model.add(Embedding(src_vocab, n_units, input_length=src_timesteps, mask_zero=True))
model.add(LSTM(n_units))
model.add(RepeatVector(tar_timesteps))
model.add(LSTM(n_units, return_sequences=True))
model.add(TimeDistributed(Dense(tar_vocab, activation='softmax')))
return model
# load datasets
dataset = load_clean_sentences('latin-bok-both.pkl')
train = load_clean_sentences('latin-bok-train.pkl')
test = load_clean_sentences('latin-bok-test.pkl')
# prepare english tokenizer
latin_tokenizer = create_tokenizer(dataset[:, 0])
latin_vocab_size = len(latin_tokenizer.word_index) + 1
latin_length = max_length(dataset[:, 0])
print('latin Vocabulary Size: %d' % latin_vocab_size)
print('latin Max Length: %d' % (latin_length))
# prepare german tokenizer
bok_tokenizer = create_tokenizer(dataset[:, 1])
bok_vocab_size = len(bok_tokenizer.word_index) + 1
bok_length = max_length(dataset[:, 1])
print('bok Vocabulary Size: %d' % bok_vocab_size)
print('bok Max Length: %d' % (bok_length))
# prepare training data
trainX = encode_sequences(bok_tokenizer, bok_length, train[:, 1])
trainY = encode_sequences(latin_tokenizer, latin_length, train[:, 0])
trainY = encode_output(trainY, latin_vocab_size)
# prepare validation data
testX = encode_sequences(bok_tokenizer, bok_length, test[:, 1])
testY = encode_sequences(latin_tokenizer, latin_length, test[:, 0])
testY = encode_output(testY, latin_vocab_size)
# define model
model = define_model(bok_vocab_size, latin_vocab_size, bok_length, latin_length, 256)
model.compile(optimizer='adam', loss='categorical_crossentropy')
# summarize defined model
print(model.summary())
# fit model
filename = 'translator.h5'
checkpoint = ModelCheckpoint(filename, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
model.fit(trainX, trainY, epochs=30, batch_size=64, validation_data=(testX, testY), callbacks=[checkpoint], verbose=2)
# load a clean dataset
def load_clean_sentences(filename):
return load(open(filename, 'rb',encoding='utf-8'))
# fit a tokenizer
def create_tokenizer(lines):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(lines)
return tokenizer
# max sentence length
def max_length(lines):
return max(len(line.split()) for line in lines)
# encode and pad sequences
def encode_sequences(tokenizer, length, lines):
# integer encode sequences
X = tokenizer.texts_to_sequences(lines)
# pad sequences with 0 values
X = pad_sequences(X, maxlen=length, padding='post')
return X
# map an integer to a word
def word_for_id(integer, tokenizer):
for word, index in tokenizer.word_index.items():
if index == integer:
return word
return None
# generate target given source sequence
def predict_sequence(model, tokenizer, source):
prediction = model.predict(source, verbose=0)[0]
integers = [argmax(vector) for vector in prediction]
target = list()
for i in integers:
word = word_for_id(i, tokenizer)
if word is None:
break
target.append(word)
return ' '.join(target)
# evaluate the skill of the model
def evaluate_model(model, tokenizer, sources, raw_dataset):
actual, predicted = list(), list()
for i, source in enumerate(sources):
# translate encoded source text
source = source.reshape((1, source.shape[0]))
translation = predict_sequence(model, latin_tokenizer, source)
raw_target, raw_src = raw_dataset[i]
if i < 10:
print('src=[%s], target=[%s], predicted=[%s]' % (raw_src, raw_target, translation))
actual.append([raw_target.split()])
predicted.append(translation.split())
# calculate BLEU score
# load datasets
dataset = load_clean_sentences('latin-bok-both.pkl')
train = load_clean_sentences('latin-bok-train.pkl')
test = load_clean_sentences('latin-bok-test.pkl')
# prepare english tokenizer
latin_tokenizer = create_tokenizer(dataset[:, 0])
latin_vocab_size = len(latin_tokenizer.word_index) + 1
latin_length = max_length(dataset[:, 0])
# prepare german tokenizer
bok_tokenizer = create_tokenizer(dataset[:, 1])
bok_vocab_size = len(bok_tokenizer.word_index) + 1
bok_length = max_length(dataset[:, 1])
# prepare data
trainX = encode_sequences(bok_tokenizer, bok_length, train[:, 1])
testX = encode_sequences(bok_tokenizer, bok_length, test[:, 1])
# load model
model = load_model('translator2.h5')
# test on some training sequences
print('train')
evaluate_model(model, latin_tokenizer, trainX, train)
# test on some test sequences
print('test')
evaluate_model(model, latin_tokenizer, testX, test)
The model works fine and is accurate. Now i wanted to implement my model to do this task: What i want to do is to get all the tokens of a doc and "translate" or rather "predict" them and store the translation in the doc object. As i can't do that i tasked a freelance developer to do this but i'm getting desperated as nothing is done correctly. The developer wrote me this code but nothing works:
code.py
from typing import List
from thinc.api import TensorFlowWrapper
from keras.models import load_model
from thinc.api import Model
from spacy.tokens.doc import Doc
import spacy
from spacy.lookups import Lookups
import os
import json
from spacy.pipeline import TrainablePipe
from spacy.language import Language
from itertools import islice
from typing import Optional
from collections.abc import Callable
from collections.abc import Iterable, Iterator
from spacy.training import Example
#spacy.registry.architectures("LatinBok.v1")
def create_tensorflow_model() -> Model[List[Doc], List[str]]:
model = load_model('translator.h5')
wrapped_model = TensorFlowWrapper(model)
return wrapped_model
#spacy.registry.misc("mon_lookups_loader")
def load_lookups(data_path):
lookups = Lookups()
# "lemma_lookup", "lemma_rules", "lemma_exc", "lemma_index"
with open(os.path.join('lemma/mon_lookup_lemma.json'), 'r',encoding="utf-8") as lemma_lookup:
lookups.add_table('lemma_lookup', json.load(lemma_lookup))
return lookups
class TrainableComponent(TrainablePipe):
def __init__(self, vocab, model, name="translator"):
self.model = model
self.vocab = vocab
self.name = name
def predict(self, docs):
return self.model.predict(docs)
def set_annotations(self, docs, scores):
c = 0
get_instances = self.model.attrs["get_instances"]
for doc in docs:
for (e1, e2) in get_instances(doc):
offset = (e1.start, e2.start)
if offset not in doc._.trans:
doc._.trans[offset] = {}
for j,token in enumerate(doc):
doc._.trans[offset][token] = scores[c, j]
c += 1
def initialize(
self,
get_examples: Callable[[], Iterable[Example]],
*,
nlp: Language = None,
labels: Optional[List[str]] = None,
):
if labels is not None:
for label in labels:
self.add_label(label)
else:
for example in get_examples():
relations = example.reference._.trans
for indices, label_dict in relations.items():
for label in label_dict.keys():
self.add_label(label)
subbatch = list(islice(get_examples(), 10))
doc_sample = [eg.reference for eg in subbatch]
label_sample = self._examples_to_truth(subbatch)
self.model.initialize(X=doc_sample, Y=label_sample)
#Language.factory("translator")
def make_component(nlp, name, model):
return TrainableComponent(nlp.vocab, model, name=name)
and he also rewrote my config.cfg file into:
[paths]
train = null
dev = null
vectors = null
init_tok2vec = null
[system]
gpu_allocator = null
seed = 0
[nlp]
lang = "bok"
pipeline = ["tok2vec","tagger","morphologizer","lemmatizer","translator"]
batch_size = 1000
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"#tokenizers":"spacy.Tokenizer.v1"}
[components]
[components.lemmatizer]
factory = "lemmatizer"
mode = "lookup"
model = null
overwrite = false
scorer = {"#scorers":"spacy.lemmatizer_scorer.v1"}
[components.morphologizer]
factory = "morphologizer"
extend = false
overwrite = true
scorer = {"#scorers":"spacy.morphologizer_scorer.v1"}
[components.morphologizer.model]
#architectures = "spacy.Tagger.v1"
nO = null
[components.morphologizer.model.tok2vec]
#architectures = "spacy.Tok2VecListener.v1"
width = ${components.tok2vec.model.encode.width}
upstream = "*"
[components.tagger]
factory = "tagger"
neg_prefix = "!"
overwrite = false
scorer = {"#scorers":"spacy.tagger_scorer.v1"}
[components.tagger.model]
#architectures = "spacy.Tagger.v1"
nO = null
[components.tagger.model.tok2vec]
#architectures = "spacy.Tok2VecListener.v1"
width = ${components.tok2vec.model.encode.width}
upstream = "*"
[components.tok2vec]
factory = "tok2vec"
[components.tok2vec.model]
#architectures = "spacy.Tok2Vec.v2"
[components.tok2vec.model.embed]
#architectures = "spacy.MultiHashEmbed.v2"
width = ${components.tok2vec.model.encode.width}
attrs = ["NORM","PREFIX","SUFFIX","SHAPE"]
rows = [5000,2500,2500,2500]
include_static_vectors = true
[components.tok2vec.model.encode]
#architectures = "spacy.MaxoutWindowEncoder.v2"
width = 256
depth = 8
window_size = 1
maxout_pieces = 3
[components.translator]
factory = "translator"
[components.translator.model]
#architectures = "LatinBok.v1"
[corpora]
[corpora.dev]
#readers = "spacy.Corpus.v1"
path = ${paths.dev}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null
[corpora.train]
#readers = "spacy.Corpus.v1"
path = ${paths.train}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null
[training]
dev_corpus = "corpora.dev"
train_corpus = "corpora.train"
seed = ${system.seed}
gpu_allocator = ${system.gpu_allocator}
dropout = 0.1
accumulate_gradient = 1
patience = 1600
max_epochs = 0
max_steps = 20000
eval_frequency = 200
frozen_components = []
annotating_components = []
before_to_disk = null
[training.batcher]
#batchers = "spacy.batch_by_words.v1"
discard_oversize = false
tolerance = 0.2
get_length = null
[training.batcher.size]
#schedules = "compounding.v1"
start = 100
stop = 1000
compound = 1.001
t = 0.0
[training.logger]
#loggers = "spacy.ConsoleLogger.v1"
progress_bar = false
[training.optimizer]
#optimizers = "Adam.v1"
beta1 = 0.9
beta2 = 0.999
L2_is_weight_decay = true
L2 = 0.01
grad_clip = 1.0
use_averages = false
eps = 0.00000001
learn_rate = 0.001
[training.score_weights]
tag_acc = 0.33
pos_acc = 0.17
morph_acc = 0.17
morph_per_feat = null
lemma_acc = 0.33
[pretraining]
[initialize]
vectors = ${paths.vectors}
init_tok2vec = ${paths.init_tok2vec}
vocab_data = null
lookups = null
before_init = null
after_init = null
[initialize.components]
[initialize.tokenizer]
[initialize.components.lemmatizer]
[initialize.components.lemmatizer.lookups]
#misc = "bok_lookups_loader"
data_path = 'lemma'
[initialize.components.translator]
and told me to retrain the model using the Boktrain.spacy and Bokdev.spacy (which were connlu files which i used to train the original model) and a json containing the lemmas.
He told me to write this in my command line to compile everything:
python -m spacy train config.cfg --output Bok --paths.train Boktrain.spacy --paths.dev Bokdev.spacy --code code.py
Which i did but i got this error:
[2022-05-13 15:01:37,487] [INFO] Set up nlp object from config
[2022-05-13 15:01:37,753] [INFO] Pipeline: ['tok2vec', 'tagger', 'morphologizer', 'lemmatizer', 'translator']
[2022-05-13 15:01:45,619] [INFO] Created vocabulary
[2022-05-13 15:01:45,630] [INFO] Finished initializing nlp object
Traceback (most recent call last):
File "C:\Users\User\anaconda3\envs\BokModel\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\User\anaconda3\envs\BokModel\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\__main__.py", line 4, in <module>
setup_cli()
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\cli\_util.py", line 71, in setup_cli
command(prog_name=COMMAND)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\click\core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\click\core.py", line 1055, in main
rv = self.invoke(ctx)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\click\core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\click\core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\typer\main.py", line 500, in wrapper
return callback(**use_params) # type: ignore
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\cli\train.py", line 45, in train_cli
train(config_path, output_path, use_gpu=use_gpu, overrides=overrides)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\cli\train.py", line 72, in train
nlp = init_nlp(config, use_gpu=use_gpu)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\training\initialize.py", line 84, in init_nlp
nlp.initialize(lambda: train_corpus(nlp), sgd=optimizer)
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\language.py", line 1309, in initialize
proc.initialize(get_examples, nlp=self, **p_settings)
File "C:\Users\User\Downloads\BokModel\LatinToArabic\test.py", line 65, in initialize
relations = example.reference._.trans
File "C:\Users\User\anaconda3\envs\BokModel\lib\site-packages\spacy\tokens\underscore.py", line 47, in __getattr__
raise AttributeError(Errors.E046.format(name=name))
AttributeError: [E046] Can't retrieve unregistered extension attribute 'trans'. Did you forget to call the `set_extension` method?
I'm desperatly trying just to get a spacy doc object with the translations in it, so that i can use that in my spacy model pipeline or by using a custom spacy extension. I have basic spacy/python understanding, but i'm not understanding anything here. I tried to compile the code with each section and it seems that only the lemma part is correct, everything else seems to be incorrect. If anyone has a solution to this, as simple as it may seem for you, i'm interested.
Your error just indicates that that particular field isn't registered. You can register it using this - put it at the top of your code.py, after the imports:
Doc.set_extension("trans")
I don't know if you have other problems or not, but that will fix your current error.
If you don't understand something, I would recommend you search the spaCy docs for related topics - there's a lot of documented examples.

TypeError('Keyword argument not understood:', kwarg) TypeError: ('Keyword argument not understood:', 'inputs')

The following code is for fake news detection with a dEFEND model using Tensorflow and Keras. For some reason, I keep getting an error. This is a TypeError with parameter 'inputs'. I don't understand why this error is being raised. Here is my code:
import keras
import pickle
import tensorflow as tf
from keras.models import *
from keras.layers import *
from keras import optimizers
from keras.optimizers import *
from keras.callbacks import *
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras import backend as K
from keras.utils.generic_utils import CustomObjectScope
from keras.layers import Layer
from keras import initializers
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score, roc_auc_score
from keras.utils.vis_utils import plot_model
from tqdm import tqdm
import re
import numpy as np
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
import jsonlines
def load_glove_embedding(path, dim, word_index):
embeddings_index = {}
f = open(path)
print('Generating GloVe embedding...')
for line in tqdm(f):
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
embedding_matrix = np.zeros((len(word_index) + 1, dim))
for word, i in word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all-zeros.
embedding_matrix[i] = embedding_vector
print('Loaded GloVe embedding')
return embedding_matrix
def load_glove_embedding(path, dim, word_index):
embeddings_index = {}
f = open(path)
print('Generating GloVe embedding...')
for line in tqdm(f):
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
embedding_matrix = np.zeros((len(word_index) + 1, dim))
for word, i in word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all-zeros.
embedding_matrix[i] = embedding_vector
print('Loaded GloVe embedding')
return embedding_matrix
class AttLayer(Layer):
"""
Attention layer used for the calcualting attention in word and sentence levels
"""
def __init__(self, **kwargs):
super(AttLayer, self).__init__(**kwargs)
self.init = initializers.get('normal')
self.supports_masking = True
self.attention_dim = 100
def build(self, input_shape):
assert len(input_shape) == 3
self.W = K.variable(self.init((input_shape[-1], self.attention_dim)))
self.b = K.variable(self.init((self.attention_dim,)))
self.u = K.variable(self.init((self.attention_dim, 1)))
self._trainable_weights = [self.W, self.b, self.u]
super(AttLayer, self).build(input_shape)
def compute_mask(self, inputs, mask=None):
return mask
def call(self, x, mask=None):
# size of x :[batch_size, sel_len, attention_dim]
# size of u :[batch_size, attention_dim]
# uit = tanh(xW+b)
uit = K.tanh(K.bias_add(K.dot(x, self.W), self.b))
ait = K.dot(uit, self.u)
ait = K.squeeze(ait, -1)
ait = K.exp(ait)
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
ait *= K.cast(mask, K.floatx())
print(ait)
ait /= K.cast(K.sum(ait, axis=1, keepdims=True) + K.epsilon(), K.floatx())
ait = K.expand_dims(ait)
weighted_input = x * ait
output = K.sum(weighted_input, axis=1)
return output
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[-1])
TOKENIZER_STATE_PATH = './saved_models/tokenizer.p'
GLOVE_EMBEDDING_PATH = './saved_models/zhs_wiki_glove.vectors.100d.txt'
class Metrics(Callback):
def __init__(self):
self.log_file = open('./data/Log_Defend_Weibo_content.txt', 'a', encoding='utf-8')
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
self.val_auc = []
self.val_acc = []
def on_epoch_end(self, epoch, logs={}):
val_predict_onehot = (
np.asarray(self.model.predict([self.validation_data[0], self.validation_data[1]]))).round()
val_targ_onehot = self.validation_data[2]
val_predict = np.argmax(val_predict_onehot, axis=1)
val_targ = np.argmax(val_targ_onehot, axis=1)
_val_f1 = f1_score(val_targ, val_predict)
_val_recall = recall_score(val_targ, val_predict)
_val_precision = precision_score(val_targ, val_predict)
_val_auc = roc_auc_score(val_targ, val_predict)
_val_acc = accuracy_score(val_targ, val_predict)
self.val_f1s.append(_val_f1)
self.val_recalls.append(_val_recall)
self.val_precisions.append(_val_precision)
self.val_auc.append(_val_auc)
self.val_acc.append(_val_acc)
print("Epoch: %d - val_accuracy: % f - val_precision: % f - val_recall % f val_f1: %f auc: %f" % (
epoch, _val_acc, _val_precision, _val_recall, _val_f1, _val_auc))
self.log_file.write(
"Epoch: %d - val_accuracy: % f - val_precision: % f - val_recall % f val_f1: %f auc: %f\n" % (epoch,
_val_acc,
_val_precision,
_val_recall,
_val_f1,
_val_auc))
return
class Defend():
def __init__(self):
self.model = None
self.MAX_SENTENCE_LENGTH = 120
self.MAX_SENTENCE_COUNT = 50
self.MAX_COMS_COUNT = 150
self.MAX_COMS_LENGTH = 120
self.VOCABULARY_SIZE = 0
self.word_embedding = None
self.model = None
self.word_attention_model = None
self.sentence_comment_co_model = None
self.tokenizer = None
self.class_count = 2
self.metrics = Metrics()
# Variables for calculating attention weights
self.news_content_word_level_encoder = None
self.comment_word_level_encoder = None
self.news_content_sentence_level_encoder = None
self.comment_sequence_encoder = None
self.co_attention_model = None
def _generate_embedding(self, path, dim):
return load_glove_embedding(path, dim, self.tokenizer.word_index)
def _build_model(self, n_classes=2, embedding_dim=100, embeddings_path=False, aff_dim=80):
GLOVE_DIR = "."
embeddings_index = {}
f = open('./saved_models/zhs_wiki_glove.vectors.100d.txt', encoding='utf-8')
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
word_index = self.tokenizer.word_index
embedding_matrix = np.random.random((len(word_index) + 1, embedding_dim))
for word, i in word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
embedding_layer = Embedding(len(word_index) + 1,
embedding_dim,
weights=[embedding_matrix],
input_length=self.MAX_SENTENCE_LENGTH,
trainable=True,
mask_zero=True)
com_embedding_layer = Embedding(len(word_index) + 1,
embedding_dim,
weights=[embedding_matrix],
input_length=self.MAX_COMS_LENGTH,
trainable=True,
mask_zero=True)
sentence_input = Input(shape=(self.MAX_SENTENCE_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sentence_input)
embedded_sequences = Lambda(lambda x: x, output_shape=lambda s: s)(embedded_sequences)
l_lstm = Bidirectional(GRU(100, return_sequences=True), name='word_lstm')(Reshape((120, 100))(embedded_sequences))
l_att = AttLayer(name='word_attention')(l_lstm)
sentEncoder = Model(sentence_input, l_att)
plot_model(sentEncoder, to_file='SentenceEncoder.png', show_shapes=True)
self.news_content_word_level_encoder = sentEncoder
content_input = Input(shape=(self.MAX_SENTENCE_COUNT, self.MAX_SENTENCE_LENGTH), dtype='int32')
content_encoder = TimeDistributed(sentEncoder)(content_input)
l_lstm_sent = Bidirectional(GRU(100, return_sequences=True), name='sentence_lstm')(content_encoder)
self.news_content_sentence_level_encoder = Model(content_input, l_lstm_sent)
content_input_ = Lambda(lambda x: tf.expand_dims(x, axis=3))(l_lstm_sent) # for channels
pooling_layer = keras.layers.AveragePooling2D(inputs=content_input_, pool_size=2, strides=2, padding='valid', data_format='channels_last',name='average_pooling_content')
preds = Dense(2, activation='softmax')(pooling_layer)
model = Model(inputs=content_input_, outputs=preds)
model.summary()
plot_model(model, to_file='CHATT_content.png', show_shapes=True)
optimize = optimizers.RMSprop(lr=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=optimize,
metrics=['accuracy'])
return model
def train(self, train_x, train_y, val_x, val_y,
batch_size=20, epochs=10,
embeddings_path=False,
saved_model_dir='saved_models', saved_model_filename=None, ):
# Fit the vocabulary set on the content and comments
self._fit_on_texts_and_comments(train_x, val_x)
self.model = self._build_model(
n_classes=train_y.shape[-1],
embedding_dim=100,
embeddings_path=embeddings_path)
# Create encoded input for content and comments
encoded_train_x = self._encode_texts(train_x)
encoded_val_x = self._encode_texts(val_x)
"""encoded_train_c = self._encode_comments(train_c)
encoded_val_c = self._encode_comments(val_c)"""
callbacks = []
callbacks.append(LambdaCallback(
on_epoch_end=lambda epoch, logs: self._save_tokenizer_on_epoch_end(
os.path.join(saved_model_dir, self._get_tokenizer_filename(saved_model_filename)), epoch))
)
if saved_model_filename:
callbacks.append(
ModelCheckpoint(
filepath=os.path.join(saved_model_dir, saved_model_filename),
monitor='val_loss',
save_best_only=True,
save_weights_only=False,
)
)
callbacks.append(self.metrics)
early_stopping = EarlyStopping(
monitor='val_acc',
min_delta=0.0005,
patience=5,
verbose=1,
restore_best_weights=True,
)
lr_scheduler = ReduceLROnPlateau(
monitor='val_acc',
factor=0.5,
patience=3,
min_lr=0.00002,
verbose=1,
)
callbacks.append(early_stopping)
callbacks.append(lr_scheduler)
print(type(callbacks))
self.model.fit(encoded_train_x, y=train_y,
validation_data=(encoded_val_x, val_y),
batch_size=batch_size,
epochs=epochs,
verbose=1,
callbacks=callbacks)
if __name__ == '__main__':
# dataset used for training
VALIDATION_SPLIT = 0.25
contents = []
#comments = []
labels = []
#texts = []
ids = []
with open('./data/fake_release_all.json', 'r+', encoding='utf-8') as json_file:
for item in jsonlines.Reader(json_file):
sentences = sent_tokenize(item['content'])
j = 0
k = 0
for sentence in sentences:
#print(sentence)
sentence = clean_str(sentence)
k+=len(sentence)
#print(sentence)
sentences[j] = sentence
j += 1
#print(sentences)
if k>=100:
contents.append(sentences)
ids.append(item['id'])
labels.append(item['label'])
else:
continue
with open('./data/real_release_all.json', 'r+', encoding='utf-8') as json_file:
for item in jsonlines.Reader(json_file):
sentences = sent_tokenize(item['content'])
j = 0
k = 0
for sentence in sentences:
sentence = clean_str(sentence)
k+=len(sentence)
sentences[j] = sentence
j += 1
if k>=100:
contents.append(sentences)
ids.append(item['id'])
labels.append(item['label'])
else:
continue
labels = np.asarray(labels)
labels = to_categorical(labels)
content_ids = set(ids)
id_train, id_test, x_train, x_val, y_train, y_val= train_test_split(ids,contents, labels,
test_size=VALIDATION_SPLIT, random_state=42,
stratify=labels)
# Train and save the model
SAVED_MODEL_FILENAME = 'Defend_Weibo_model_content.h5'
h = Defend()
h.train(x_train, y_train, x_val, y_val,
batch_size=20,
epochs=30,
embeddings_path='./zhs_wiki_glove.vectors.100d.txt',
saved_model_dir=SAVED_MODEL_DIR,
saved_model_filename=SAVED_MODEL_FILENAME)
And here is the error:
Tensor("word_attention/Exp:0", shape=(?, 120), dtype=float32)
Tensor("time_distributed_1/word_attention/Exp:0", shape=(?, ?), dtype=float32)
Traceback (most recent call last):
File"C:/Users/hqint/PycharmProjects/pythonProject/7460/dEFEND/fake_news_detection/dEFEND/defend_content.py", line 684, in <module>saved_model_filename=SAVED_MODEL_FILENAME)
File "C:/Users/hqint/PycharmProjects/pythonProject/7460/dEFEND/fake_news_detection/dEFEND/defend_content.py", line 380, in train embeddings_path=embeddings_path)
File "C:/Users/hqint/PycharmProjects/pythonProject/7460/dEFEND/fake_news_detection/dEFEND/defend_content.py", line 271, in _build_model
pooling_layer = keras.layers.AveragePooling2D(inputs=content_input_, pool_size=2, strides=2, padding='valid', data_format='channels_last',name='average_pooling_content')
File "C:\Users\hqint\PycharmProjects\pythonProject\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\hqint\PycharmProjects\pythonProject\venv\lib\site-packages\keras\layers\pooling.py", line 317, in __init__
data_format, **kwargs)
File "C:\Users\hqint\PycharmProjects\pythonProject\venv\lib\site-packages\keras\layers\pooling.py", line 171, in __init__
super(_Pooling2D, self).__init__(**kwargs)
File "C:\Users\hqint\PycharmProjects\pythonProject\venv\lib\site-packages\keras\engine\base_layer.py", line 128, in __init__
raise TypeError('Keyword argument not understood:', kwarg)
TypeError: ('Keyword argument not understood:', 'inputs')
How can I fix this TypeError? I have installed TensorFlow 1.13.1 and Keras 2.2.4. It seems that the input tensor of the average_pooling layer is the error, but I don't know how to fix it.

Creating an ai chatbot, but getting a traceback error

I'm trying to create an ai chatbox in python. I tried following this tutorial: https://techwithtim.net/tutorials/ai-chatbot/part-1/ but I'm getting a lot of errors of deprecations and getting some Traceback error.
Here's the code:
import json
import random
import tensorflow
import tflearn
import numpy
import sys
import pickle
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
nltk.download('punkt')
with open("trainingData.json") as file:
data = json.load(file)
try:
with open("data.pickle", "rb") as f:
words, labels, training, output = pickle.load(f)
except:
words = []
labels = []
docs_x = []
docs_y = []
for intent in data["intents"]:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_x.append(wrds)
docs_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
words = [stemmer.stem(w.lower()) for w in words if w != "?"]
words = sorted(list(set(words)))
labels = sorted(labels)
training = []
output = []
out_empty = [0 for _ in range(len(labels))]
for x, doc in enumerate(docs_x):
bag = []
wrds = [stemmer.stem(w.lower()) for w in doc]
for w in words:
if w in wrds:
bag.append(1)
else:
bag.append(0)
output_row = out_empty[:]
output_row[labels.index(docs_y[x])] = 1
training.append(bag)
output.append(output_row)
training = numpy.array(training)
output = numpy.array(output)
with open("data.pickle", "wb") as f:
pickle.dump((words, labels, training, output), f)
tensorflow.reset_default_graph()
net = tflearn.input_data(shape=[None, len(training[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i, w in enumerate(words):
if w == se:
bag[i] = 1
return numpy.array(bag)
def chat():
print("Start talking with the bot (type quit to stop)!")
while True:
inp = input("You: ")
if inp.lower() == "quit":
break
results = model.predict([bag_of_words(inp, words)])
results_index = numpy.argmax(results)
tag = labels[results_index]
for tg in data["intents"]:
if tg['tag'] == tag:
responses = tg['responses']
print(random.choice(responses))
chat()
Here are the errors I'm getting. How can I fix the deprecation errors, the traceback error?
Here's the text of the error:
Run id: VOB3W4
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 20
Validation samples: 0
--
--
Traceback (most recent call last):
File "script.py", line 91, in <module>
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
File "/usr/local/lib/python2.7/site-packages/tflearn/models/dnn.py", line 216, in fit
callbacks=callbacks)
File "/usr/local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 339, in fit
show_metric)
File "/usr/local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 816, in _train
tflearn.is_training(True, session=self.session)
File "/usr/local/lib/python2.7/site-packages/tflearn/config.py", line 95, in is_training
tf.get_collection('is_training_ops')[0].eval(session=session)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 731, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 5579, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 950, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1096, in _run
raise RuntimeError('Attempted to use a closed Session.')
RuntimeError: Attempted to use a closed Session.
At start file "model.tflearn" doesn't exist and try/except should catch error when code try to load this file and run fit() and save()
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
but it seems this error closes tf.session() so it can't run fit()correctly.
If you remove try/except with load() and keep only fit() and save() then it has no problem to create model and save it in file.
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
After creating file "model.ftlearn" you can use again try/except with load() and it should work if you don't delete file with model.
Better solution should check if file exists - but it saves data in few files "model.tflearn.index", "model.tflearn.meta" and "model.tflearn.data-00000-of-00001" so it should check one of this file instead of "model.tflearn"
Use
import os
if os.path.exists("model.tflearn.meta"):
model.load("model.tflearn")
else:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
instead of
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
EDIT: It seems this problem exists at least 2 years: RuntimeError: Attempted to use a closed Session in tflearn
try doing this in this:
try:
model.load("model3.tflearn")
except:
model = tflearn.DNN(net)
model.fit(training,output, n_epoch = 1000, batch_size = 8, show_metric = True)
model.save("model3.tflearn")

Categories