简体   繁体   中英

CNN Model unable to train

I'm trying to train a CNN model to give a prediction of format

array([ 0., 0., 0., 0., -1.], dtype=float32) .

My Training data looks like this :

0        [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 0.0, 0....
1        [[0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 1.0, 0....
2        [[0.0, 0.0, 0.0, -1.0, -1.0], [0.0, 0.0, 0.0, ...
3        [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, ...
4        [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 0.0, 0....
                               ...                        
15484    [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 2.0, 1....
15485    [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, ...
15486    [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 2.0, 0....
15487    [[1.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0....
15488    [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, ...

With each row of shape (24,5) looking like this :

array([[-1., -1., -1., -1., -1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  3.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 0.,  1.,  2.,  0.,  0.],
       [ 0.,  3.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0., -1.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 1.,  2.,  0.,  0., -1.]], dtype=float32)

The model I'm using looks like this :

model = tf.keras.Sequential(layers=[
                tf.keras.layers.Conv2D(32,kernel_size = 3, activation=tf.nn.relu, input_shape=(28,28,1)),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(32,kernel_size = 3, activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(32,kernel_size = 5,strides=2,padding='same', activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),

                tf.keras.layers.Conv2D(64,kernel_size = 3, activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(64,kernel_size = 3, activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(64,kernel_size = 3, strides=2, padding='same', activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Flatten(),
                tf.keras.layers.Dense(128,activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Dense(1, activation=tf.nn.softmax)])

        model.compile(optimizer=tf.keras.optimizers.Adam(0.1), 
                      loss=tf.keras.losses.CategoricalCrossentropy(), 
                      metrics=tf.keras.metrics.Accuracy())

I'm new to the field and is currently getting the following error :

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

Can someone tell me what I'm doing wrong here? I added a tf.convert_to_tensor() function on the training data but is still getting the same error.

Adding .astype("float32") seems to not be working either.

The tf.convert_to_tensor() function should actually have done it. Your toy-example array converted likethis a_tensor = tf.convert_to_tensor(a, dtype=tf.int32) would be a valid input for the CNN, as would be an array of n such arrays (then with shape=(n, 24, 5) ). You could post a minimal working example of your code, so we can check for syntax ... for now it is hard to tell what went wrong.

Apart from that two small observations: Your input shape in the CNN does not yet show the right shape fitting your data. And the last dense softmax layer is also not yet what your proposed prediction format looks like.

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