问题描述
我正在尝试使用 Keras LSTM 进行二元分类。
我的输入数据是 2340 条记录 * 254 个特征。
输出为 1*2340。
下面是我的代码。
X_res = np.array(X_res)
X_res = np.reshape(X_res,([1,2340,254]))
y_res = np.array(y_res)
y_res = np.reshape(y_res,2340]))
y_test = np.array(y_test)
y_test = np.reshape(y_test,314]))
model = keras.Sequential()
model.add(keras.layers.LSTM(32,input_dim = 254,return_sequences=True))
model.add(keras.layers.LSTM(100))
model.add(keras.layers.Dense(100,activation='sigmoid'))
model.add(keras.layers.Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=[tf.keras.metrics.Recall()])
model.fit(X_res,y_res,epochs= 5)
但是我无法克服错误:
ValueError:logits 和标签必须具有相同的形状 ((None,1) vs (None,2340))
解决方法
您需要更改最后一层以匹配所需的输出形状(从 1 到 2340)。通过运行下面的示例代码,您会看到它在实践中是如何工作的:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
#let's simulate...
X_res=np.random.rand(2340,254)
y_res=np.random.rand(1,2340)
y_test=np.random.rand(1,314)
X_res = np.array(X_res)
X_res = np.reshape(X_res,([1,2340,254]))
y_res = np.array(y_res)
y_res = np.reshape(y_res,2340]))
y_test = np.array(y_test)
y_test = np.reshape(y_test,314]))
model = keras.Sequential()
model.add(keras.layers.LSTM(32,input_dim = 254,return_sequences=True))
model.add(keras.layers.LSTM(100))
model.add(keras.layers.Dense(100,activation='sigmoid'))
model.add(keras.layers.Dense(2340,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=[tf.keras.metrics.Recall()])
model.summary()
#test a littlebit before training...
test_input=tf.ones((1,254))
print("Test result of original model: ",model(test_input).numpy())
#train...
model.fit(X_res,y_res,epochs= 5)
#test after training...just to show the shapes of inputs and outputs. Change the number of inputs you want to test to see the effect...
how_many_inputs_you_want_to_test=1
X_test=np.random.rand(how_many_inputs_you_want_to_test,254)
print("Input shape: ",X_test.shape)
y_pred = (model.predict(X_test) > 0.5).astype("int32")
print("Output shape: ",y_pred.shape)
,
建议如何在此问题案例中重塑张量:
#About tensors and reshaping
import tensorflow as tf
#The tensor need to be reshaped:
example_tensor=tf.ones((314,254))
try:
#The reshaping is done by the command:
example_tensor_reshaped=tf.reshape(example_tensor,[314,254])
except:
print("Sorry,this conversion is not possible!")
#----> which produce an error!
#Why? Because the reshaping is possible only with compatible sizes,for example conversion:
print("...thus let's try again with a different conversion...")
#The tensor need to be reshaped:
example_tensor=tf.ones((314,254*2340))
#The reshaping is done by the command:
example_tensor_reshaped=tf.reshape(example_tensor,254])
#----> which happens succesfully!
print("The shape of reshaped tensor: ",example_tensor_reshaped.shape)