Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

2024/11/16 9:35:21

How to fix the input array to meet the input shape?

I tried to transpose the input array, as described here, but an error is the same.

ValueError: Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

import tensorflow as tf
import numpy as npmodel = tf.keras.models.Sequential([tf.keras.layers.Dense(40, input_shape=(21,), activation=tf.nn.relu),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(1, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])arrTest1 = np.array([0.1,0.1,0.1,0.1,0.1,0.5,0.1,0.0,0.1,0.6,0.1,0.1,0.0,0.0,0.0,0.1,0.0,0.0,0.1,0.0,0.0])
scores = model.predict(arrTest1)
print(scores)
Answer

Your test array, arrTest1, is a 1d vector of 21:

>>> arrTest1.ndim
1

What you are trying to feed your model is a row of 21 features. You simply need one more set of brackets:

arrTest1 = np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.1, 0., 0.1, 0.6, 0.1, 0.1, 0., 0., 0., 0.1, 0., 0., 0.1, 0., 0.]])

And now you have a row with 21 values:

>>> arrTest1.shape
(1, 21)
https://en.xdnf.cn/q/71361.html

Related Q&A

Sort order when loading related objects using selectinload in SQLAlchemy

Is there a way to specify the sort order when loading related objects using the selectinload option in SQLAlchemy?My SQLAlchemy version: 1.2.10 My python version: 3.6.6

How to implement autovivification for nested dictionary ONLY when assigning values?

TL;DR How can I get superkeys to be autovivified in a Python dict when assigning values to subkeys, without also getting them autovivified when checking for subkeys?Background: Normally in Python, se…

How can I iterate across the photos on my connected iPhone from Windows 7 in Python?

When I connect my iPhone to my Windows 7 system, the Windows Explorer opens a Virtual Folder to the DCIM content. I can access the shell library interface via Pywin32 (218) as mentioned here: Can I use…

Why doesnt the python slice syntax wrap around from negative to positive indices?

I noticed, given l = [1,2,3], that l[-1:] returns [3] as expected, but that l[-1:0] returns [], very much unlike what I expected. I then tried [-1:1], which I expected to return [3,1], but it also retu…

How do I modify a generator in Python?

Is there a common interface in Python that I could derive from to modify behavior of a generator?For example, I want to modify an existing generator to insert some values in the stream and remove some…

Reading a website with asyncore

I would like to read a website asynchronously, which isnt possible with urllib as far as I know. Now I tried reading with with plain sockets, but HTTP is giving me hell. I run into all kind of funky en…

How to select specific the cipher while sending request via python request module

Usecase: I want to find out how many ciphers are supported by the hostname with python request module.I am not able to find a way to provide the cipher name to request module hook. Can anyone suggest …

Different classes made by type with the same name in Python?

I was playing around with metaclasses in Python and found something very curious. I can create two classes with the same name, but that are actually different objects. See:>>> def create_class…

Installing python server for emacs-jedi

I am trying to install Jedi for emacs using marmalade package manager by following instructions here -- http://tkf.github.io/emacs-jedi/latest/. The package manger installs Jedi along with its dependen…

Multi-feature causal CNN - Keras implementation

Im currently using a basic LSTM to make regression predictions and I would like to implement a causal CNN as it should be computationally more efficient.Im struggling to figure out how to reshape my cu…