简体   繁体   English

ValueError:检查输入时出错:预期dense_1_input的形状为(1,)但得到的数组形状为(5000,)

[英]ValueError: Error when checking input: expected dense_1_input to have shape (1,) but got array with shape (5000,)

I am trying to build an NLP model for Sarcasm detection dataset on kaggle.I am a beginner in implementing neural networks and this is the first time I am implementing neural network using Keras.Here is my Code:我正在尝试为 kaggle 上的讽刺检测数据集构建 NLP model。我是实现神经网络的初学者,这是我第一次使用 Keras 实现神经网络。这里是我的代码:

# Neural Network.
import tensorflow as tf
print(tf.__version__)

import keras
print(keras.__version__)

from keras.models import Sequential
from keras.layers import Dense

# Create a new sequential model.
model = Sequential()

# Add input layer and Dense layer.
# Input layer contains 1 feature whereas first hidden layer has 5 neurons.
model.add(Dense(5,input_shape=(1,),activation="relu"))

# Add a final output one neuron layer.
model.add(Dense(1,activation="sigmoid"))

# Summarize a model:
model.summary()

# Model output shape.
print(model.output_shape)

# Model config.
print(model.get_config())

# List all weight tensors.
print(model.get_weights())

# Compile the Model.
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

# Fit the model.
model.fit(vector_array_train,y_train,epochs=20,batch_size=1, verbose=1)

Output for the code is: Output 的代码是:

2.0.0
Using TensorFlow backend.
2.3.1
2020-08-01 18:58:02.364557: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 5)                 10        
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 6         
=================================================================
Total params: 16
Trainable params: 16
Non-trainable params: 0
_________________________________________________________________
(None, 1)
{'name': 'sequential_1', 'layers': [{'class_name': 'Dense', 'config': {'name': 'dense_1', 'trainable': True, 'batch_input_shape': (None, 1), 'dtype': 'float32', 'units': 5, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'mode': 'fan_avg', 'distribution': 'uniform', 'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}, {'class_name': 'Dense', 'config': {'name': 'dense_2', 'trainable': True, 'dtype': 'float32', 'units': 1, 'activation': 'sigmoid', 'use_bias': True, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'mode': 'fan_avg', 'distribution': 'uniform', 'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}]}
[array([[-0.85931516,  0.5637381 , -0.6789112 ,  0.1663289 ,  0.1063652 ]],
      dtype=float32), array([0., 0., 0., 0., 0.], dtype=float32), array([[ 0.6834357 ],
       [ 0.5921519 ],
       [-0.71200275],
       [ 0.13235688],
       [ 0.589782  ]], dtype=float32), array([0.], dtype=float32)]
Traceback (most recent call last):
  File "C:/Users/sumed/Desktop/DataScience_Projects/NLPProjects/Sarcasm_Detection/sarcasm_detection.py", line 180, in <module>
    model.fit(vector_array_train,y_train,epochs=20,batch_size=1, verbose=1)
  File "C:\Python3.6.6\lib\site-packages\keras\engine\training.py", line 1154, in fit
    batch_size=batch_size)
  File "C:\Python3.6.6\lib\site-packages\keras\engine\training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "C:\Python3.6.6\lib\site-packages\keras\engine\training_utils.py", line 145, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected dense_1_input to have shape (1,) but got array with shape (5000,)


I am getting an error at last code line: ValueError: Error when checking input: expected dense_1_input to have shape (1,) but got array with shape (5000,)我在最后一行代码出现错误:ValueError: Error when checks input: expected dense_1_input to have shape (1,) but got array with shape (5000,)

As a newbie,I am not able to understand, whats wrong here?作为一个新手,我无法理解,这里有什么问题? Please help.请帮忙。

When you defined the model, you told the model to expect an input of shape (1,) here: model.add(Dense(5,input_shape=(1,),activation="relu"))当您定义 model 时,您告诉 model 期望在此处输入形状 (1,): model.add(Dense(5,input_shape=(1,),activation="relu"))

change it to model.add(Dense(5,input_shape=(None,),activation="relu")) .将其更改为model.add(Dense(5,input_shape=(None,),activation="relu")) Alternatively you can also set it to 5000 instead of None或者,您也可以将其设置为 5000 而不是None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期density_1_input的形状为(24,),但数组的形状为(1,) - ValueError: Error when checking input: expected dense_1_input to have shape (24,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input 具有形状(9,) 但得到形状为(1,) 的数组 - ValueError: Error when checking input: expected dense_1_input to have shape (9,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input 具有形状(8,) 但得到形状为(1,) 的数组 - ValueError: Error when checking input: expected dense_1_input to have shape (8,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input具有形状(180,)但得到形状为(1,)的数组 - ValueError: Error when checking input: expected dense_1_input to have shape (180,) but got array with shape (1,) ValueError:检查时出错:预期dense_1_input具有形状(3,)但得到形状为(1,)的数组 - ValueError: Error when checking : expected dense_1_input to have shape (3,) but got array with shape (1,) ValueError:检查时出错:预期density_1_input具有2维,但数组的形状为(1,16,16,512) - ValueError: Error when checking : expected dense_1_input to have 2 dimensions, but got array with shape (1, 16, 16, 512) 如何修复&#39;ValueError:检查输入时出错:期望dense_1_input有形状(4,)但是在Python中有错误的形状(1,)数组? - How to fix ‘ValueError: Error when checking input: expected dense_1_input to have shape (4,) but got array with shape (1,)’ error in Python? ValueError:检查输入时出错:预期density_1_input具有形状(1,224,224),但数组形状为(224,224,1) - ValueError: Error when checking input: expected dense_1_input to have shape (1, 224, 224) but got array with shape (224, 224, 1) 检查输入时出错:预期density_1_input具有形状(3773),但数组的形状为(111,) - Error when checking input: expected dense_1_input to have shape (3773,) but got array with shape (111,) 检查输入时出错:预期dense_1_input 具有形状(784,) 但得到形状为(10,) 的数组 - Error when checking input: expected dense_1_input to have shape (784,) but got array with shape (10,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM