Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

machine learning - Tensorflow neural network does not work, incompatible types

This is my code:

X_train, Y_train, X_test, Y_test = load_data(DATA_PATH)

model = keras.Sequential([

    # input layer
    # 1st dense layer
    keras.layers.Dense(256, activation='relu', input_shape=(X_train.shape[1], X_train.shape[2],  X_train.shape[3])),

    # 2nd dense layer
    keras.layers.Dense(128, activation='relu'),

    # 3rd dense layer
    keras.layers.Dense(64, activation='relu'),

    # output layer
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.summary()

classifier = model.fit(X_train,
                       Y_train,
                       epochs=100,
                       batch_size=128)

Y_train ,X_train and Y_test ,X_test are numpy arrays. X_train contains 800 and X_test 200 .png pictures of size 128X128. Y_train contains 800 labels (80x1, 80x2, etc.) and Y_test contains testing target (20x1, 20x2, etc.).

When I try to run this program I get the following error:

ValueError: Shapes (None, 1) and (None, 128, 128, 10) are incompatible
question from:https://stackoverflow.com/questions/65902112/tensorflow-neural-network-does-not-work-incompatible-types

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to reshape your input Here is a running code

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

X_train = tf.random.normal(shape=(800,128,128,3))
X_test = tf.random.normal(shape=(200,128,128,3))
Y_train = tf.random.normal(shape=(800,10))
Y_test = tf.random.normal(shape=(200,10))

#reshape
X_train = tf.reshape(X_train, shape=(800, 128*128*3))



model = keras.Sequential([

    # input layer
    # 1st dense layer
    keras.layers.Dense(256, activation='relu', input_shape=(X_train.shape[0], X_train.shape[1])),

    # 2nd dense layer
    keras.layers.Dense(128, activation='relu'),

    # 3rd dense layer
    keras.layers.Dense(64, activation='relu'),

    # output layer
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.summary()

classifier = model.fit(X_train,
                       Y_train,
                       epochs=100,
                       batch_size=128)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...