我仔细检查了代码,但发生了错误
“ TypeError:预期的字节,找到浮点数” 我正在使用python 3.6,windows10 看起来像model.fit无法正常工作
数据集是涉及“文本”和“标签”的新闻 使用以下代码预处理数据。 帮我PLZ〜
def dat_embedding(path,vector_dimension = 300): 数据= pd.read_csv(路径)
missing_rows = []
for i in range(len(data)):
if data.loc[i, 'text'] != data.loc[i, 'text']:
missing_rows.append(i)
data = data.drop(missing_rows).reset_index().drop(['index','id'],axis=1)
for i in range(len(data)):
data.loc[i, 'text'] = cleanup(data.loc[i,'text'])
del_rows = [] # to-do
for i in range (len (data)):
if data.loc[i, 'label'] != 0 and \
data.loc[i, 'label'] != 1:
del_rows.append (i)
data = data.drop(del_rows, axis=0)
x = constructLabeledSentences(data['text'])
y = data['label'].values #fake or not
text_model = Doc2Vec(min_count=1, window=5, vector_size=vector_dimension, sample=1e-4, negative=5, workers=7, epochs=10,
seed=1)
text_model.build_vocab(x)
text_model.train(x, total_examples=text_model.corpus_count, epochs=text_model.iter)
train_size = int(0.8 * len(x))
test_size = len(x) - train_size
text_train_arrays = np.zeros((train_size, vector_dimension))
text_test_arrays = np.zeros((test_size, vector_dimension))
train_labels = np.zeros(train_size)
test_labels = np.zeros(test_size)
for i in range(train_size):
text_train_arrays[i] = text_model.docvecs['Text_' + str(i)]
train_labels[i] = y[i]
j = 0
for i in range(train_size, train_size + test_size):
text_test_arrays[j] = text_model.docvecs['Text_' + str(i)]
test_labels[j] = y[i]
j = j + 1
return text_train_arrays, text_test_arrays, train_labels, test_labels
def ELMoEmbedding(x):
return elmo(tf.squeeze(tf.cast(x, tf.string)), as_dict=True, signature="default")["default"]
input_text = Input(shape=(300,), dtype=tf.string)
embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ))
(input_text)
print("************************")
print(input_text.shape) # (?,300)
hidden_layer = Dense(256, activation='relu')(embedding_layer)
output_layer = Dense(1, activation='sigmoid')(hidden_layer)
model = Model(inputs=[input_text], outputs=output_layer)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics= ['accuracy'])
model.fit(xtr, ytr, validation_data=(xte, yte), epochs=5, batch_size=64)
scores = model.evaluate(xte, yte, verbose=0)
#
Using TensorFlow backend.
2019-07-07 19:53:01.892636:
Itensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
WARNING: Logging before flag parsing goes to stderr.
W0707 19:53:02.449947 3052 deprecation.py:323] From
C:\Users\yhe\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\ops\control_flow_ops.py:3632: colocate_with
(from tensorflow.python.framework.ops) is deprecated and will be removed
in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-07-07 19:53:03.051612: W tensorflow/core/framework/allocator.cc:124]
Allocation of 67108864 exceeds 10% of system memory.
2019-07-07 19:53:03.099789: W tensorflow/core/framework/allocator.cc:124]
Allocation of 67108864 exceeds 10% of system memory.
2019-07-07 19:53:03.501113: W tensorflow/core/framework/allocator.cc:124]
Allocation of 67108864 exceeds 10% of system memory.
2019-07-07 19:53:03.784631: W tensorflow/core/framework/allocator.cc:124]
Allocation of 67108864 exceeds 10% of system memory.
(?, 300)
(16608, 300) #x-train shape
(16608,) #y-train shape
(4153, 300) #x-test shape
(4153,) #y-test shape
W0707 20:02:27.745670 3052 deprecation.py:323] From
C:\Users\yhe\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from
tensorflow.python.ops.math_ops) is deprecated and will be removed in a
future version.
Instructions for updating:
Use tf.cast instead.
Train on 16608 samples, validate on 4153 samples
Epoch 1/5
TypeError: expected bytes, float found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\yhe\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\client\session.py", line 1439, in __call__
run_metadata_ptr)
SystemError: <built-in function TF_SessionRunCallable> returned a result
with an error set
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/yhe/Downloads/FakeBuster-master/FakeBuster-
master/ELMO.py", line 58, in <module>
model.fit(xtr, ytr, validation_data=(xte, yte), epochs=5, batch_size=64)
File "C:\Users\yhe\AppData\Roaming\Python\Python36\site-
packages\keras\engine\training.py", line 1039, in fit
validation_steps=validation_steps)
File "C:\Users\yhe\AppData\Roaming\Python\Python36\site-
packages\keras\engine\training_arrays.py", line 199, in fit_loop
outs = f(ins_batch)
File "C:\Users\yhe\AppData\Roaming\Python\Python36\site-
packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
return self._call(inputs)
File "C:\Users\yhe\AppData\Roaming\Python\Python36\site-
packages\keras\backend\tensorflow_backend.py", line 2675, in _call
fetched = self._callable_fn(*array_vals)
File "C:\Users\yhe\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\client\session.py", line 1439, in __call__
run_metadata_ptr)
File "C:\Users\yhe\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\errors_impl.py", line 528, in
__exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InternalError: Unable to get
element as bytes.
Process finished with exit code 1