簡體   English   中英

為序列模型准備數據(機器學習)

[英]Preparing data for sequential model (machine learning)

我從文件中獲取數據

y = np.genfromtxt('dataY.txt', dtype=np.float32) #input data for target dataset
x = np.genfromtxt('dataX.txt', dtype=np.float32)#input data for input dataset

我相應地拆分數據:

xtrain, xtest, ytrain, ytest = train_test_split (x,y,test_size=.2)
train_data = tf.data.Dataset.from_tensor_slices((xtrain, ytrain))
valid_data = tf.data.Dataset.from_tensor_slices((xtest, ytest))

dataY.txt文件由 1000 行組成。 給定輸入 X,每行包含 30 個我希望神經網絡在訓練后猜測的數字。

dataX.txt文件由 1000 行組成,每個 Y 一個。每行包含 100 個數字。

問題:如何使以下代碼工作?

model = Sequential()
#whad do I need to write in the following line(s)?
model.add(Conv2D(100,(7,7)))
model.add(LeakyReLU())
model.compile( loss='mse', metrics=['mse'])
model.fit(train_data, epochs=10, validation_data=valid_data)
ypred = model.predict(x)

錯誤:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (100, 1)

看起來您正在對一維數據使用卷積層。 改為嘗試密集層:

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(100,)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(30))
[...]

此外,我建議打印 x、y、xtest、xtrain 等的形狀,以確保您以正確的尺寸進料。

(來源: https : //keras.io/api/layers/core_layers/dense/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM