import keras from keras.models import Sequential from keras.layers import Dense, Activation import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import r2_score #乱数発生 x = np.random.randn(100) y = 5.2*x + np.random.randn(100)*0.8 #4個のニューロンからなる2層を構築 model = Sequential() #活性化関数はlinearとする model.add(Dense(4, input_dim = 1, activation = 'linear', name = 'layer_1')) model.add(Dense(1, activation = 'linear', name = 'layer_2')) print (model.summary()) model.compile(optimizer = 'sgd', loss = 'mse', metrics = ['accuracy']) #学習前の重みと閾値の出力 print("Weights and biases of the layers before training the model: \n") for layer in model.layers: print(layer.name) print("Weights") print("Shape: ",layer.get_weights()[0].shape,'\n',layer.get_weights()[0]) print("Bias") print("Shape: ",layer.get_weights()[1].shape,'\n',layer.get_weights()[1],'\n') #ニューラルネットワークの訓練 model.fit(x,y, batch_size = 1, epochs = 100, shuffle = False) yhat = model.predict(x) #学習後の重みと閾値の出力 print("Weights and biases of the layers after training the model with new weights and biases: \n") for layer in model.layers: print(layer.name) print("Weights") print("Shape: ",layer.get_weights()[0].shape,'\n',layer.get_weights()[0]) print("Bias") print("Shape: ",layer.get_weights()[1].shape,'\n',layer.get_weights()[1],'\n') print("R2値=", r2_score(y, yhat)) plt.figure(figsize = (8,8)) plt.xlabel('Input Variable (x)') plt.ylabel('Output Variable (y)') plt.plot(x,y,'o',x,model.predict(x),'k') plt.show()