简体   繁体   中英

Why is the TensorFlow Flatten layer not changing the input shape?

I'm new to programming neural networks and using tensorflow and have spent the last day trying to build some simple networks of my own to get practice. I have a shape of (113, 200) that I'm attempting to flatten in the first layer of a sequential network but when the Dense layer runs I receive the error

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 22600 but received input with shape [113, 200]

I have also noticed that I am recieving the warning

tensorflow:Model was constructed with shape (None, 113, 200) for input Tensor("flatten_input:0", shape=(None, 113, 200), dtype=float32), but it was called on an input with incompatible shape (113, 200).

But when I change the input_shape to (None, 113, 200) I receive other errors

Below is my current code

model_x = keras.Sequential([
    keras.layers.Flatten(input_shape=(113, 200)),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(2)
])
model_x.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                optimizer="adam",
                metrics=["accuracy"])

cp_path_x = "TestAITraining/cpx.ckpt"
cp_dir_x = os.path.dirname(cp_path_x)
cp_callback_x = keras.callbacks.ModelCheckpoint(filepath=cp_path_x, save_weights_only=True, verbose=1)
#model.load_weights(cp_path)
while True:
    model_x.fit(tf.data.Dataset.from_tensor_slices((train_arrays, train_pos)), epochs=20)

train_array is a (1000, 113, 200) python list before being passed into the dataset and train_pos is a (1000, 2) python list before being added to the data set

import tensorflow as tf
model_x = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(113, 200)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(2, activation='softmax')
])
model_x.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                optimizer="adam",
                metrics=["accuracy"])
model_x.summary()

Output

Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten_1 (Flatten)         (None, 22600)             0         
                                                                 
 dense_3 (Dense)             (None, 64)                1446464   
                                                                 
 dense_4 (Dense)             (None, 64)                4160      
                                                                 
 dense_5 (Dense)             (None, 2)                 130       
                                                                 
=================================================================
Total params: 1,450,754
Trainable params: 1,450,754
Non-trainable params: 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM