返回

python-ValueError:hiddenL1层的输入0与该层不兼容:其秩未定义,但该层需要定义秩

发布时间:2022-09-07 05:57:47 275
# node.js

我正在尝试创建一个带有自定义权重的连续keras模型。权重来自numpy数组中的一行。在运行代码时,我得到一个错误:

ValueError:hiddenL1层的输入0与该层不兼容:其秩未定义,但该层需要定义的秩。

我不确定这里还有什么有用的信息,如果您想了解更多信息,请随时发表评论。

df = pd.read_csv('NASDAQ.csv')
array = df.to_numpy()
array = np.delete(array, 0,1)
inc = []


maxs = np.max(array,axis=0)
for i in range(len(array)):
    for j in range(len(array[0])):
        array[i][j] = array[i][j]/maxs[j]


population_size = 100
output11 = 1
activation = 'elu'
out_activation='sigmoid'
epochs = 500
opt = SGD(lr=.01, momentum =.9)
weights = [[]for _ in range(population_size)]


for i in range(population_size):
    for j in range(6):
        weights[i].append(random.uniform(0, 1000))


for i in range(len(array)):
    if i == 0:
        inc.append(0)
    elif array[i][4] > array[i-1][4]:
        inc.append(1)
    elif array[i][4] <= array[i-1][4]:
        inc.append(0)
    
    
inc = np.array(inc)
inc = np.reshape(inc, (-1,1))
array = np.hstack((array, inc))


input_train = array[0:1259,0:6]
output_train = array[0:1259, 6]
input_validate = array[1259:1887, 0:6]
output_validate = array[1259:1887, 6]
input_test = array[1887: , 0:6]
output_test = array[1887: , 6]


weights1 = np.array(weights[0])
weights1.shape = (6,1)
out_test2d =np.reshape(output_test,(len(output_test),1))
out_train2d = np.reshape(output_test, (len(output_test),1))


weights1 = np.array(weights[0])
weights1.shape=(6,)
model = Sequential();
model.add(InputLayer())
model.add(Dense(6, use_bias = True, name = "hiddenL1", input_shape = (None,6)) )
model.add(Dense(output11, use_bias=True, name = "output", activation = out_activation))

opt = tf.keras.optimizers.Adam(learning_rate=0.01, epsilon=None, decay=0.05, beta_1 = 1,beta_2 = .5,amsgrad=False)
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.layers[0].set_weights([weights1])
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
下一篇
列表-打印Python中间值 2022-09-07 04:41:48