Keras错误:“ ValueError:层顺序的输入0与该层不兼容:预期的ndim = 3,找到的ndim = 2。收到的完整形状:[无,1]”

时间:2020-05-22 20:32:24

标签: python arrays numpy tensorflow keras

我一直在尝试使用Keras创建一个基本的预测神经网络。目的是采用12位数字输入并产生单个值输出。虽然结合了多个教程,但我还是生成了下面的代码,但出现了错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 1]

我打算根据需要添加更多层/更改层。我的代码:

import tensorflow as tf
from tensorflow import keras
import numpy as np
from tensorflow.keras import layers
from numpy import genfromtxt
import csv
import pandas
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
TRAINING_SIZE = 50000
OUTSIZE=1
REVERSE = True
INSIZE = 12 
i=[]
o=[]
with open('Dataset.csv')as f:
    csvin = csv.reader(f)
    for row in csvin:
        i.append(row[0])
        o.append(row[1])

input = np.asarray(i)
output = np.asarray(o)

print (i)
print (o)

split_at = len(i) - len(i) // 10
(i_train, i_val) = input[:split_at], i[split_at:]
(o_train, o_val) = output[:split_at], o[split_at:]

print (i_train)
print (o_train)

num_layers = 1
model = keras.Sequential()
model.add(layers.LSTM(128, input_shape=(12,1)))
model.add(layers.RepeatVector(1))
for _ in range(num_layers):
    model.add(layers.LSTM(128, return_sequences=True))
model.add(layers.Dense(1, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()

epochs = 30
batch_size = 4

epochs = 30
batch_size = 32

for epoch in range(1, epochs):
    print()
    print("Iteration", epoch)
    model.fit(
        i_train,
        o_train,
        batch_size=batch_size,
        epochs=1,
        validation_data=(i_val, o_val),
    )

0 个答案:

没有答案