Keras Cant似乎要配置inputShape

时间:2018-11-28 23:24:03

标签: python tensorflow machine-learning keras

我正在尝试使用Iris-Flowers制作Keras的“ Hello World”,但无法配置input_shape。这是我的错误信息:

"ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (120, 4)"

当我将input_shape更改为接收到的输入形状时,它只会更改其表示接收到的输入的内容。如您所知,此数据集有4个输入和1个输出。有人知道我将如何配置我的input_shape吗?

这是我的代码

import tensorflow as tf

text_file = open("iris.data.txt")
rawData = text_file.read().split('\n')
text_file.close()

for x in range(0,150):
    rawData[x] = rawData[x].split(',')

xs_train = []
ys_train = []
for i in range (0,40):
    ys_train.append(rawData[i][4])
    xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (50,90):
    ys_train.append(rawData[i][4])
    xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (100,140):
    ys_train.append(rawData[i][4])
    xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

xs_test = []
ys_test = []
for i in range (40,50):
    ys_test.append(rawData[i][4])
    xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (90,100):
    ys_test.append(rawData[i][4])
    xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (140,150):
    ys_test.append(rawData[i][4])
    xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

# print(xs_train)   

for i in range(0, len(ys_train)):
    if ys_train[i] == "Iris-setosa":
        ys_train[i] = [1,0,0]
    if ys_train[i] == "Iris-versicolor":
        ys_train[i] = [0,1,0]
    if ys_train[i] == "Iris-virginica":
        ys_train[i] = [0,0,1]

# print(ys_train)


model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(4, input_shape=(1,4), activation= 'relu'))
model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(xs_train, ys_train, epochs=3)

我的输入经过格式化,因此每个数组都是一个数据集,每个集合包括4个 数据点,就这样:

[['5.1', '3.5', '1.4', '0.2'],
 ['4.9', '3.0', '1.4', '0.2'],
 ['4.7', '3.2', '1.3', '0.2']...]

1 个答案:

答案 0 :(得分:0)

input_shape=(4,)

输入形状与批次大小无关,仅与“每个”样本大小有关。