这里设置x,y的关系是y=2x+3
因为输入的x是1维的数字,输出的y也是1维的,所以dense层的输出维度为1,总共的参数为(1+1)*1=2个,分别是w和b
# import packages
import numpy as np
from keras.models import *
from keras.layers import *
import matplotlib.pyplot as plt
# generate data
x_train = np.linspace(-1, 1, 200)
y_train = 2 * x_train + np.random.normal(3, 0.5, (x_train.shape[-1]))
x_test = np.linspace(-1, 1, 100)
y_test = 2 * x_test + np.random.normal(3, 0.5, (x_test.shape[-1]))
# initialize model
lr_model = Sequential()
lr_model.add(Dense(1, activation='linear', input_dim=1, name='dense_1'))
lr_model.compile(loss='mse', optimizer='sgd')
# train
lr_model.fit(x_train, y_train, batch_size=32, epochs=500, verbose=1)
# evaluate
x = lr_model.evaluate(x_test, y_test, batch_size=32)
print(lr_model.layers[0].get_weights())
print(lr_model.layers[0].get_config())
lr_model.summary()
# visualization
y_pred = lr_model.predict(x_test)
plt.scatter(x_test, y_test)
plt.plot(x_test, y_pred)
plt.show()
通过model.summary()可以查看网络的层,可以看出,总共有2个可训练的参数。
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 1) 2
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
通过get_config()和get_weights()方法可以查看layer的配置和参数,可以看出计算的kernel为2,bias为3
[array([[2.0756874]], dtype=float32), array([3.0122392], dtype=float32)]
{'name': 'dense_1', 'trainable': True, 'batch_input_shape': (None, 1), 'dtype': 'float32', 'units': 1, 'activation': 'linear', 'use_bias': True, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'mode': 'fan_avg', 'distribution': 'uniform', 'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}