0

Im Trying to train a model and im getting a error like this

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(100,100,3)),
    tf.keras.layers.Dense(128, activation="relu" ),
    tf.keras.layers.Dense(4)
])
Loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
Optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=Optimizer,
              loss=Loss,
              metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=10)

Im getting error at history:

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

I found the reason for error its becuase of X_train.shape its output is (21,) i shoud get something simliar to like (21, 100, 100, 3) How can i convert that?

Edit:

X_train[0] return this

Shape of this is (179, 282, 3)

Dats In that is

array([[[0.15686275, 0.2       , 0.14509804],
        [0.15686275, 0.18823529, 0.12941176],
        [0.21176471, 0.23921569, 0.17647059],
        ...,
        [0.9372549 , 0.95686275, 0.97254902],
        [0.9372549 , 0.94901961, 0.96862745],
        [0.95294118, 0.96470588, 0.98431373]],

       [[0.18039216, 0.21176471, 0.16078431],
        [0.12156863, 0.14509804, 0.09019608],
        [0.16470588, 0.18039216, 0.12156863],
        ...,
        [0.89411765, 0.91372549, 0.92941176],
        [0.97647059, 0.98823529, 1.        ],
        [0.97647059, 0.98823529, 1.        ]],

       [[0.22352941, 0.25490196, 0.20392157],
        [0.12156863, 0.14509804, 0.09019608],
        [0.13333333, 0.14117647, 0.08627451],
        ...,
        [0.87058824, 0.88235294, 0.90196078],
        [0.98823529, 1.        , 1.        ],
        [0.99607843, 1.        , 1.        ]],

       ...,

       [[0.16862745, 0.29019608, 0.05882353],
        [0.23921569, 0.34509804, 0.1372549 ],
        [0.23137255, 0.31764706, 0.1372549 ],
        ...,
        [0.2       , 0.31764706, 0.16862745],
        [0.58431373, 0.7372549 , 0.41176471],
        [0.55294118, 0.74117647, 0.27058824]],

       [[0.21176471, 0.33333333, 0.10196078],
        [0.27843137, 0.38431373, 0.17647059],
        [0.2745098 , 0.36078431, 0.18039216],
        ...,
        [0.27843137, 0.39607843, 0.24705882],
        [0.62352941, 0.77647059, 0.45098039],
        [0.59215686, 0.78039216, 0.30980392]],

       [[0.23529412, 0.35686275, 0.1254902 ],
        [0.29803922, 0.40392157, 0.19607843],
        [0.29803922, 0.38431373, 0.20392157],
        ...,
        [0.31764706, 0.43529412, 0.29411765],
        [0.55294118, 0.70588235, 0.38039216],
        [0.52156863, 0.70980392, 0.23921569]]])
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sunilsai
  • 68
  • 6
  • Please provide some sample data with it. – Innat Mar 04 '21 at 04:08
  • @M.Innat Ive Edited Please Check – Sunilsai Mar 04 '21 at 04:35
  • Does this answer your question? [Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)](https://stackoverflow.com/questions/58636087/tensorflow-valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupporte) – Anurag Wagh Mar 04 '21 at 05:07

1 Answers1

0

You can use the following code to convert your numpy array to tensor.

images = [[0.9372549 , 0.95686275, 0.97254902],
    [0.9372549, 0.94901961, 0.96862745],
    [0.95294118, 0.96470588, 0.98431373]]] 

# Converting list to numpy array
images = np.array(images) 

# Converting it to 'float32'
images = images.astype('float32')

# Normalize the Numpy array (if desired)
images = images / 255.0

# Convert the Numpy array to a TensorFlow tensor
images = tf.convert_to_tensor(images) 
print(images)