【问题标题】:Error when checking input: expected input_3 to have 3 dimensions, but got array with shape (860, 11)检查输入时出错:预期 input_3 有 3 个维度,但得到了形状为 (860, 11) 的数组
【发布时间】:2019-10-05 19:07:46
【问题描述】:

我收到了这个错误。

ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (860, 11)

这些是我正在使用的以下代码。 df 具有 860x15 尺寸,因此 datX 具有 860x11 尺寸

# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

df =pd.read_excel('C:/Users/ASUS/Documents/Script/Simulation.Machine.V1/Final.xlsx', sheetname= "C0")
datX = df.drop(['C0', 'C1', 'C2', 'C3'], axis=1)

import numpy as np
datY = df['C1'] / df['C0'] 
datW = df['C0']**(1/2)
datZ = df['C1']

q=20
p= len(datX.columns)
from keras import backend as K

# define the keras model
model = Sequential()
model.add(Dense(q, input_dim=p, activation='tanh'))
model.add(Dense(1, activation= K.exp))

# define the keras model
offset = Sequential()
offset.add(Dense(1, input_dim=1, activation='linear'))

from keras.layers import Input
tweet_a = Input(shape=(860, 11))
tweet_b = Input(shape=(860, 1))
tweetx = model(tweet_a)
tweety = offset(tweet_b)

from keras.layers import Multiply, add
output = Multiply()([tweetx, tweety])

from keras.models import Model
modelnew = Model(inputs=[tweet_a, tweet_b], outputs=output)

modelnew.compile(optimizer='rmsprop',loss='mse',metrics=['accuracy'])

modelnew.fit([datX, datW], datY, epochs=100, batch_size=10000)

我希望输出为 1 维,输入为 11 维

【问题讨论】:

    标签: python machine-learning keras neural-network


    【解决方案1】:

    这里发生了什么很明显。为了理解这一点,您需要了解Input 层中batch_shapeshape 参数之间的区别。

    当您指定 shape 参数时,它实际上会在形状的开头添加一个新维度(即批量维度)。因此,当您将860x11 作为shape 传递时,实际模型需要bx860x11 大小的输出(其中b 是批量大小)。您在此处指定的是 batch_shape 参数的值。所以有两种解决方案。

    对您而言,最好的解决方案是将上述内容更改为以下内容。因为这样你就不用依赖固定的批次尺寸了。

    tweet_a = Input(shape=(11,))
    tweet_b = Input(shape=(1,))
    tweetx = model(tweet_a)
    tweety = offset(tweet_b)
    

    但如果您 100% 确定批量大小始终为 860,则可以尝试以下选项。

    tweet_a = Input(batch_shape=(860, 11))
    tweet_b = Input(batch_shape=(860, 1))
    tweetx = model(tweet_a)
    tweety = offset(tweet_b)
    

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 2020-02-27
      • 2020-07-01
      • 2020-06-08
      • 2020-12-08
      • 2019-11-11
      • 2018-12-26
      • 2020-04-01
      • 2019-09-03
      相关资源
      最近更新 更多