InvalidArgumentError:类型为EluGrad的操作的输入必须具有相同的大小和形状。输入0:[1,20]!=输入1:[32,20]

时间:2019-04-29 23:50:02

标签: python tensorflow keras

我有以下代码应该是最小的重复示例

from keras.callbacks import ModelCheckpoint
from keras.layers import advanced_activations
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error 
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import warnings 
warnings.filterwarnings('ignore')
warnings.filterwarnings('ignore', category=DeprecationWarning)
from keras.models import Model
from keras.layers import Dense, Input, Add, Lambda
from keras.models import Model
from keras.layers import Dense, Input, Add, Lambda
import numpy as np
import scipy as sci
import pandas as pd
import scipy.stats
from sklearn import *
from sklearn.svm import SVR
import tensorflow as tf


def model(inp_size ):
    inp = Input(batch_shape=(1 , inp_size))

    x1 = Dense(50, activation='elu')((inp))
    x1 = Dense(20, activation='elu')(x1)
    x1 = Dense(1, activation = 'linear')(x1)

    x2 = Dense(50, activation='elu')(inp)
    x2 = Dense(20, activation='elu')(x2)
    x2 = Dense(1, activation = 'linear')(x2)

    x3 = Dense(50, activation='elu')(inp)
    x3 = Dense(20, activation='elu')(x3)
    x3 = Dense(1, activation = 'linear')(x3)

    x4 = Dense(50, activation='elu')(inp)
    x4 = Dense(20, activation='elu')(x4)
    x4 = Dense(1, activation = 'linear')(x4)



    x1 = Lambda(lambda x: x * 4)(x1)
    x2 = Lambda(lambda x: x * 5)(x2)
    x3 = Lambda(lambda x: x * 6)(x3)
    x4 = Lambda(lambda x: x * 6)(x4)

    out = Add()([x1, x2, x3, x4])

    return Model(inputs = inp, outputs = out)
X_train = np.random.rand(3000,46)
y_train = np.random.rand(3000,1)

NN_model = model(X_train.shape[1])
NN_model.compile(loss='mean_absolute_error', optimizer='SGD', metrics=['mean_absolute_error'])
NN_model.build(X_train.shape)

NN_model.summary()
NN_model.fit(X_train, y_train, epochs=2,verbose = 1)

我需要比较输出与单个值的线性组合,因此这是我建议的结构。但是,当我运行代码时,出现以下错误:

InvalidArgumentError: Matrix size-incompatible: In[0]: [32,20], In[1]: [1,1]
     [[{{node training_50/SGD/gradients/dense_1173/MatMul_grad/MatMul_1}}]]

谢谢您的帮助!

0 个答案:

没有答案