美好的一天!我想用Keras python库训练神经网络 我想制作4个输入神经元和1个输出神经元。我想使用 我自己的带有数字的csv文件:这是它
my_5_input_numbers.csv
0.3,0.5,0.6,0.7,1
0.4,0.6,0.7,0.8,0
0.5,0.7,0.8,0.9,1
我使用numpy来读取csv并制作列车矩阵。这是代码和 错误
import numpy as np
np.set_printoptions(threshold=np.inf)
from keras.datasets import boston_housing
from keras.models import Sequential
from keras.layers import Dense
data_common=np.genfromtxt('my_5_input_numbers.csv',delimiter=',')
"""
data_common=array([[ 0.3, 0.5, 0.6, 0.7, 1. ],
[ 0.4, 0.6, 0.7, 0.8, 0. ],
[ 0.5, 0.7, 0.8, 0.9, 1. ]])
data_common.shape=(3,5)
"""
X_train=data_common[:,-1]#X_train.shape=(3,4)
y_train=data_common[0:4,-1]#y_train.shape=(3,)
y_train=y_train.reshape(3,1)
model = Sequential()
model.add(Dense(128,input_dim=4,activation='relu'))
model.add(Dense(1,activation="softmax"))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# train nn
model.fit(X_train, y_train, batch_size=200, epochs=25, validation_split=0.2, verbose=2)
#<---Error:File "D:\NetbeansPythonProjects\testDiffrentCode\src\testKeras.py", line 16, in <module>
# model.fit(X_train, y_train, batch_size=200, epochs=25, validation_split=0.2, verbose=2)
#ValueError:
#Error when checking input:
#expected dense_1_input to have shape (None, 4) but got array with shape (3, 1)
答案 0 :(得分:0)
你只是搞砸了你的输入:
X_train=data_common[:,-1] # <--- X_train.shape was actually (3,) not (3,4).
y_train=data_common[0:4,-1] # <--- This was wrong as well.
y_train=y_train.reshape(3,1)
应该
X_train=data_common[:,0:4]
y_train=data_common[:,-1]
y_train=y_train.reshape(3,1)
numpy的索引首先是行,然后是列。