错误节点:' binary_crossentropy/cast'火车模型时不支持将绳子施加到浮动
我想训练我的数据,我已经将数据从此处使用Word2Vec Pretrain模型串起,https://dl.fbaipublicfiles.com/fasttext/fasttext/fasttext/vectors-crawl/cc.id.300.vec.gz.gz
成功制作模型,但是当我想训练数据集时,我会遇到这样的错误
UnimplementedError Traceback (most recent call last)
<ipython-input-28-85ce60cd1ded> in <module>()
1 history = model.fit(X_train, y_train, epochs=6,
2 validation_data=(X_test, y_test),
----> 3 validation_steps=30)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
UnimplementedError: Graph execution error:
#skiping error
Node: 'binary_crossentropy/Cast'
Cast string to float is not supported
[[{{node binary_crossentropy/Cast}}]] [Op:__inference_train_function_21541]
:
file = gzip.open(urlopen('https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.id.300.vec.gz'))
vocab_and_vectors = {}
# put words as dict indexes and vectors as words values
for line in file:
values = line.split()
word = values [0].decode('utf-8')
vector = np.asarray(values[1:], dtype='float32')
vocab_and_vectors[word] = vector
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
# how many features should the tokenizer extract
features = 500
tokenizer = Tokenizer(num_words = features)
# fit the tokenizer on our text
tokenizer.fit_on_texts(df["Comment"].tolist())
# get all words that the tokenizer knows
word_index = tokenizer.word_index
print(len(word_index))
# put the tokens in a matrix
X = tokenizer.texts_to_sequences(df["Comment"].tolist())
X = pad_sequences(X)
# prepare the labels
y = df["sentiments"].values
# split in train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, stratify=y, random_state=30)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
embedding_matrix = np.zeros((len(word_index) + 1, 300))
for word, i in word_index.items():
embedding_vector = vocab_and_vectors.get(word)
# words that cannot be found will be set to 0
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Embedding
model = tf.keras.Sequential([
tf.keras.layers.Embedding(len(word_index)+1, 300, input_length=X.shape[1], weights=[embedding_matrix], trainable=False),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(loss='binary_crossentropy',
optimizer=tf.keras.optim
izers.Adam(1e-4),
metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=6,
validation_data=(X_test, y_test),
validation_steps=30)
i want to train my data i already make my data to string with word2vec pretrain model from here https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.id.300.vec.gz
and success to make a model, but when i want to train the dataset i got error like this
UnimplementedError Traceback (most recent call last)
<ipython-input-28-85ce60cd1ded> in <module>()
1 history = model.fit(X_train, y_train, epochs=6,
2 validation_data=(X_test, y_test),
----> 3 validation_steps=30)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
UnimplementedError: Graph execution error:
#skiping error
Node: 'binary_crossentropy/Cast'
Cast string to float is not supported
[[{{node binary_crossentropy/Cast}}]] [Op:__inference_train_function_21541]
the code :
file = gzip.open(urlopen('https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.id.300.vec.gz'))
vocab_and_vectors = {}
# put words as dict indexes and vectors as words values
for line in file:
values = line.split()
word = values [0].decode('utf-8')
vector = np.asarray(values[1:], dtype='float32')
vocab_and_vectors[word] = vector
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
# how many features should the tokenizer extract
features = 500
tokenizer = Tokenizer(num_words = features)
# fit the tokenizer on our text
tokenizer.fit_on_texts(df["Comment"].tolist())
# get all words that the tokenizer knows
word_index = tokenizer.word_index
print(len(word_index))
# put the tokens in a matrix
X = tokenizer.texts_to_sequences(df["Comment"].tolist())
X = pad_sequences(X)
# prepare the labels
y = df["sentiments"].values
# split in train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, stratify=y, random_state=30)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
embedding_matrix = np.zeros((len(word_index) + 1, 300))
for word, i in word_index.items():
embedding_vector = vocab_and_vectors.get(word)
# words that cannot be found will be set to 0
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Embedding
model = tf.keras.Sequential([
tf.keras.layers.Embedding(len(word_index)+1, 300, input_length=X.shape[1], weights=[embedding_matrix], trainable=False),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(loss='binary_crossentropy',
optimizer=tf.keras.optim
izers.Adam(1e-4),
metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=6,
validation_data=(X_test, y_test),
validation_steps=30)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于,您将字符串作为类(“ positif”,“ negativ”),如您的数据摘录所示。只是将它们转换为
,它应该起作用。
如果不是,我们将不得不更深入地了解您的代码。
编辑:
因此,由于怀疑字符串作为类的问题。您可以将它们更改为float:
在此之后,您还应该将
y-nparry
float的dtype更改为float:这应该可以解决问题。
有关参考,另请参见我的
I think the problem is, that you are having Strings as classes ("positif", "negativ") as shown in your data excerpt. Just transform them to
and it should work.
If not we would have to look a little bit deeper into your code.
EDIT:
So as suspected the problem where the Strings as classes. You can change them to float with:
After this you also should change the dtype of the
y-nparry
to float:This should do the trick.
For reference see also my colab code
我通过将Y属于对象类型的Y火车标签转换为INT类型来解决问题。
像这样 !
在此处输入图像描述
I resolved the issue by converting the y train labels, which were of object type, to int type.
like this !
enter image description here