gpt4 book ai didi

python - Keras神经网络: ValueError - input shape is wrong

转载 作者:行者123 更新时间:2023-11-30 09:40:34 26 4
gpt4 key购买 nike

我正在尝试编写一个小型回归神经网络作为学习基础知识的起点。

这是我正在使用的简单数据集:

/image/xx6mm.png

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import pandas as pd
import io
import os
import requests
import numpy as np
from sklearn import metrics

df = pd.read_csv("C:\\Users\\Dan\\y_sinx.csv")

x = df['x'].values #Pandas to Numpy
y = df['y'].values


print(type(x)) #check type
print(np.shape(x)) #check dimensions
print(x) #check x

#Network
model = Sequential()
model.add(Dense(7, input_shape = x.shape, activation='relu')) #Hidden layer 1
model.add(Dense(4, activation='relu')) #Hidden layer 2
model.add(Dense(1)) #Output layer
model.compile(loss='mean_squared_error', optimizer = 'adam')
model.fit(x, y, verbose = 2, epochs = 20)

此代码给出输出:

<class 'numpy.ndarray'>
(7,)
[0. 0.78539816 1.57079633 2.35619449 3.14159265 3.92699082
4.71238898]

所以它看起来是正确的大小(7,),但也许 x 本身的输出看起来是错误的,它应该是一列?我收到错误:

ValueError                                Traceback (most recent call last)
<ipython-input-1-5db977397f3e> in <module>
24 model.add(Dense(1)) #Output layer
25 model.compile(loss='mean_squared_error', optimizer = 'adam')
---> 26 model.fit(x, y, verbose = 2, epochs = 20)
27
28 #Prediction

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
641 max_queue_size=max_queue_size,
642 workers=workers,
--> 643 use_multiprocessing=use_multiprocessing)
644
645 def evaluate(self,

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
630 steps=steps_per_epoch,
631 validation_split=validation_split,
--> 632 shuffle=shuffle)
633
634 if validation_data:

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
2426 feed_input_shapes,
2427 check_batch_axis=False, # Don't enforce the batch size.
-> 2428 exception_prefix='input')
2429
2430 if y is not None:

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
519 ': expected ' + names[i] + ' to have shape ' +
520 str(shape) + ' but got array with shape ' +
--> 521 str(data_shape))
522 return data
523

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

我不确定它是如何获得形状为 (1,) 的数组以及如何修复它,我们将不胜感激!

最佳答案

Keras 期望输入层中 X 的属性或变量数量,但您将输入层定义为

model.add(Dense(7, input_shape = x.shape, activation='relu')) #Hidden layer 1

所以,这意味着输入层中将有 7 个隐藏单元,这不应该是真的,因为 X 中只有 1 个变量。尝试这样做:

model.add(Dense(1, input_dim = x.shape[0], activation='relu')) #Input layer

关于python - Keras神经网络: ValueError - input shape is wrong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com