python tensorflow代码错误ValueError:调用图层“sequential”(类型Sequential)时遇到异常

xxls0lw8  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(576)

“从本地文件中学习。有4760个训练数据和240个测试数据。我得到这些错误,有什么问题?”

train_dir=''
    train_csv = pd.read_csv(train_dir+'train.csv')
    train_images=[]
    train_labels=[]
    
    for file in train_csv['data']:
        image=np.array(Image.open(train_dir+'train/'+file))
        train_images.append(image)
    for label in train_csv['label']:
        train_labels.append(label)
    
    train_images=np.array(train_images)
    train_labels=np.array(train_labels)
    
    test_dir=''
    test_csv = pd.read_csv(test_dir+'test.csv')
    
    test_images=[]
    test_labels=[]
    
    for file in test_csv['data']:
        image=np.array(Image.open(test_dir+'test/'+file))
        test_images.append(image)
    for label in test_csv['label']:
        test_labels.append(label)
    
    test_images=np.array(test_images)
    test_labels=np.array(test_labels)
    
    import numpy as np
    import tensorflow as tf
    from tensorflow.keras.datasets import fashion_mnist
    
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    from tensorflow.keras.optimizers import SGD,Adam,Adagrad,RMSprop
    
    
    print("train_images",train_images.shape)
    print("train_labels",train_labels.shape)
    
    
    print("test_images",test_images.shape)
    print("test_labels",test_labels.shape)
    
    train_images=train_images.reshape(4760,4096,3)
    test_images=test_images.reshape(240,4096,3)
    
    
    x_train=train_images.astype(np.float32)/255.0
    x_test=test_images.astype(np.float32)/255.0
    
    
    
    
    y_train=tf.keras.utils.to_categorical(train_labels,5)
    y_test=tf.keras.utils.to_categorical(test_labels,5)
    
    
    print("x_train",x_train.shape)
    print("y_train",y_train.shape)
    
    
    print("x_test",x_test.shape)
    print("y_test",y_test.shape)
    
    n_input=4096
    n_hidden1=5000
    n_hidden2=2500
    n_hidden3=2500
    n_hidden4=2500
    n_output=5
    
    batch_siz=256
    n_epoch=5
    
    
    def build_model():
        model=Sequential()
        model.add(Dense(units=n_hidden1,activation='relu',input_shape=(n_input,)))
        model.add(Dense(units=n_hidden2,activation='relu'))
        model.add(Dense(units=n_hidden3,activation='relu'))
        model.add(Dense(units=n_hidden4,activation='relu'))
        model.add(Dense(units=n_output,activation='softmax'))
        return model
    
    
    
    dmlp_adam=build_model()
    dmlp_adam.compile(loss='categorical_crossentropy',optimizer=Adam(),metrics=['accuracy'])
    hist_adam=dmlp_adam.fit(x_train,y_train,batch_size=batch_siz,epochs=n_epoch,validation_data=(x_test,y_test),verbose=2)
error:WARNING:tensorflow:Model was constructed with shape (None, 4096) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4096), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (None, 4096, 3).

ValueError: Exception encountered when calling layer "sequential" (type Sequential).
    
    Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 4096, but received input with shape (None, 4096, 3)
    
    Call arguments received by layer "sequential" (type Sequential):
      • inputs=tf.Tensor(shape=(None, 4096, 3), dtype=float32)
      • training=True
      • mask=None
kwvwclae

kwvwclae1#

选择太多的神经元可能会导致过拟合,增加网络的训练时间。隐层神经元(单元)的数量最好介于输入层和输出层的大小之间。请在下面找到工作代码。

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten
import numpy as np

x_train=tf.random.uniform((4760,4096,3))
x_test=tf.random.uniform((240,4096,3))

# It generates labels randomly within the range of 0 to 5
import random
def Rand(start, end, num):
    res = []
 
    for j in range(num):
        res.append(random.randint(start, end))
 
    return res

# Train and Test labels
y_test=np.array(Rand(0,4,240))
y_train=np.array(Rand(0,4,4760))    

y_train=tf.keras.utils.to_categorical(y_train)
y_test=tf.keras.utils.to_categorical(y_test)

print("x_train",x_train.shape)
print("y_train",y_train.shape)    
print("x_test",x_test.shape)
print("y_test",y_test.shape)

# Changing the input shape from 4096 to (4096,3) resolves the issue as the input is of shape (4096,3).
n_input=(4096,3)  
n_hidden1= 128  #5000
n_hidden2= 64  #2500
n_hidden3= 32  #2500
n_hidden4= 16  #2500
n_output=5

batch_siz=256
n_epoch=5
    
def build_model():
    model=Sequential()
    model.add(Dense(units=n_hidden1,activation='relu',input_shape=(n_input)))
    model.add(Dense(units=n_hidden2,activation='relu'))
    model.add(Dense(units=n_hidden3,activation='relu'))
    model.add(Dense(units=n_hidden4,activation='relu'))

    # Added flatten layer to overcome ValueError: Shapes (None, 5) and (None, 4096, 5) are incompatible
    model.add(Flatten())

    model.add(Dense(units=n_output,activation='softmax'))
    return model    

model=build_model()   
model.summary()     
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=batch_siz,epochs=n_epoch,validation_data=(x_test,y_test),verbose=2)

相关问题