我想訓練我的資料我已經從這里使用 word2vec 預訓練模型將我的資料字串化https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.id.300.vec.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)
我的資料:
uj5u.com熱心網友回復:
我認為問題在于,您將字串作為類(“positif”,“negativ”),如您的資料摘錄中所示。只需將它們轉換為
負值 = 0正值= 1
它應該可以作業。如果不是,我們將不得不更深入地研究您的代碼。
編輯: 所以懷疑字串作為類的問題。您可以將它們更改為浮動:
df["sentiments"].loc[df["sentiments"]=="positif"]=1.0
df["sentiments"].loc[df["sentiments"]=="negatif"]=0.0
在此之后,您還應該將 dtype 更改y-nparry為浮動:
y = np.asarray(y).astype("float64")
這應該可以解決問題。作為參考,另請參閱我的colab 代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/485292.html
標籤:Python 机器学习 nlp lstm tf.keras
上一篇:我們如何自動檢測資料中的偏斜并且存在偏斜,那么我們如何將其移除?
下一篇:IONIC/REACT第9:5行:期望一個賦值或函式呼叫,而是看到一個運算式@typescript-eslint/no-unused-expressions
