我正在测试一个基础的神经网络模型。但在继续之前,我遇到了如截图所示的错误。
这是我的代码:
import numpy as np
# Training Data
x_train = np.array([[1.0,1.0]])
y_train = np.array([2.0])
for i in range(3,10000,2):
x_train = np.append(x_train,[[i,i]],axis = 0)
y_train = np.append(y_train,[i+i],axis = 0)
# Test Data
import numpy as np
x_test = np.array([[2.0,2.0]])
y_test = np.array([4.0])
for i in range(4,8000,4):
x_test = np.append(x_test,[[i,i]],axis = 0)
y_test = np.append(y_test,[i+i])
from tensorflow import keras
from keras.layers import Flatten # to flatten the input data
from keras.layers import Dense # for the hidden layer
# We'll follow sequential method i.e. one after the other(input layer ---> hidden layer---> output layer)
model = keras.Sequential()
# For input layer
model.add(Flatten(input_shape = x_train[0].shape)) # input layer
# For Hidden layer
model.add(Dense(2,activation = 'relu')) # '2' represents a no. of neurons
# For Output layer
model.add(Dense(1)) # By default, activation = 'linear'
# before training
bf_train = model.get_weights()
bf_train
错误提示为:
ValueError: 模型 sequential 的权重尚未创建。当模型首次使用输入数据调用或调用带有 input_shape 参数的 build()
时,权重将被创建。
tf 2.x
,那么您应该知道keras
不再是一个独立的库,而是tf
的一部分。 - Innat