简体   繁体   English

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,)

I am trying to train a neural network to bounce a ball and I am having a problem predicting the ball's motion, getting the error ValueError: Error when checking input: expected dense_1_input to have shape (8,) but got array with shape (1,)我正在尝试训练神经网络来弹跳球,但在预测球的运动时遇到问题,出现错误ValueError: Error when checking input: expected dense_1_input to have shape (8,) but got array with shape (1,)
My code:我的代码:

from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import csv
import pygame

running=True

def main():
    # load training data
    data_path = 'x.dat'
    with open(data_path, 'r') as f:
        reader = csv.reader(f, delimiter=',')
        headers = next(reader)
        x_train = np.array(list(reader)).astype(float)
    data_path = 'y.dat'
    with open(data_path, 'r') as f:
        reader = csv.reader(f, delimiter=',')
        headers = next(reader)
        y_train = np.array(list(reader)).astype(float)
    
    # debug print statement
    print(x_train)
    
    # define the keras model
    model = Sequential()
    model.add(Dense(8, input_shape=(8,), activation='relu'))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(4, activation='relu'))

    # compile the keras model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    # fit the keras model on the dataset
    model.fit(x_train, y_train, epochs=1, batch_size=1000)

    # pygame initialization to visualize the ball
    global running, screen

    pygame.init()
    screen = pygame.display.set_mode((200, 200))
    pygame.display.set_caption("BallPhysics")
    screen.fill((255,255,255))
    pygame.display.update()

    # ball info
    x = 25
    y = 10
    xVel = -2
    yVel = 0
    gravity = 0.1
    elasticity = 0.9999
    radius = 10
    friction = 0.999

    while running:
        screen.fill((255,255,255))
        ev = pygame.event.get()

        # draw ball
        pygame.draw.circle(screen, (255, 0, 0), (x,y), radius)
        pygame.display.update()

        # make input array
        inp = [x/200,y/200,((xVel/10)+1)/2,((yVel/10)+1)/2,gravity,elasticity,radius/50.0,friction]
        print(inp)
        out = model.predict(inp)

        # set ball position and velocity to NN output
        x = out[0][0]
        y = out[0][1]
        xVel = out[0][2]
        yVel = out[0][3]
        
        # event handling
        for event in ev:
            if event.type == pygame.QUIT:
                running = False
                pygame.display.quit()
                pygame.quit()
        
            pygame.display.flip()

main()

The debug print statement prints out调试打印语句打印出来

[[0.025568 0.131659 0.755605 ... 0.414219 0.094692 0.678865]
...
[0.08742  0.08742  0.5      ... 0.250432 0.699359 0.179118]]

which is slightly confusing because one thing I noticed is that the printed out arrays have no commas, while the arrays I make do have commas.这有点令人困惑,因为我注意到的一件事是打印出来的数组没有逗号,而我制作的数组有逗号。 This may have something to do with it but I don't know what.这可能与它有关,但我不知道是什么。 Any help is appreciated.任何帮助表示赞赏。

Stack trace:堆栈跟踪:

Traceback (most recent call last):
  File "/Users/grimtin10/Documents/Python Projects/BallPhysics/BallPhysics.py", line 78, in <module>
    main()
  File "/Users/grimtin10/Documents/Python Projects/BallPhysics/BallPhysics.py", line 63, in main
    out = model.predict(inp)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 1149, in predict
    x, _, _ = self._standardize_user_data(x)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
    exception_prefix='input')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected dense_1_input to have shape (8,) but got array with shape (1,)

Alright, so, I figured it out.好吧,所以,我想通了。 As it turns out, the NN was expecting batch input, causing a single array to not work.事实证明,神经网络期待批量输入,导致单个数组不起作用。 I am not super familiar with tf and keras so this was the problem.我对 tf 和 keras 不是很熟悉,所以这就是问题所在。 Using np.reshape(inp,(1,8)) fixed it.使用np.reshape(inp,(1,8))修复它。

暂无
暂无

声明:本站的技术帖子网页,遵循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的形状为(1,)但得到的数组形状为(5000,) - ValueError: Error when checking input: expected dense_1_input to have shape (1,) but got array with shape (5000,) 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