#!/usr/bin/python3 # -*- coding: utf-8 -*- #   import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from pandas import DataFrame from tensorflow.keras import layers, models import sys import time import specrep t = time.localtime() print('executed at {:4}/{:02}/{:02} {:02}:{:02}:{:02}'.format(t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec)) print('python version:', sys.version) print('numpy version:', np.__version__) print('tensorflow version: ', tf.__version__) if (not tf.__version__.startswith('2.')): print('This code requires version 2.*.') exit() print('\nPrepare the training dateset.') train_t = np.array([5.2, 5.7, 8.6, 14.9, 18.2, 20.4, 25.5, 26.4, 22.8, 17.5, 11.1, 6.6]) train_t = train_t.reshape([12, 1]) train_x = np.array([[mon**n for n in range(1, 5)] for mon in range(1, 13)]) print('train_x =', train_x) print('train_t =', train_t) # x を5次元の入力データとみなし、12組の(x,t)のサンプルと理解する print('\nDefine a model to predict labels using a polynomal function.') model = models.Sequential() model.add(layers.Dense(1, input_shape=(4,), name='polynomial')) model.summary() print('\nCompile the model using the Adam optimizer, and MSE (Mean Square Error) as a loss function.') model.compile(optimizer='adam', loss='mse') print('\nApply the optimization for 10 epochs.') history = model.fit(train_x, train_t, batch_size=12, epochs=10) print('\nApply the optimization for additional 100,000 epochs.') time_start = time.perf_counter() history = model.fit(train_x, train_t, batch_size=12, epochs=100000, verbose=0) time_end = time.perf_counter() print('time elapsed =', time_end-time_start) print('\nPlot charts to see how the loss has changed.') DataFrame({'loss': history.history['loss']}).plot(xlim=(0, 2000)) plt.savefig('ex1-2a.png') #plt.show() DataFrame({'loss': history.history['loss']}).plot(ylim=(0, 10)) plt.savefig('ex1-2b.png') #plt.show() print('\nShow weights after the training.') print('weights =', model.get_weights()) print('\nDefine a function to predict values using the final weights.') w, b = model.get_weights() def predict(x): pred = b[0] + sum([w[n][0] * x**(n+1) for n in range(0, 4)]) return pred print('\nPlot a chart for predictions.') fig = plt.figure() subplot = fig.add_subplot(1, 1, 1) subplot.set_xlim(1, 12) subplot.scatter(range(1, 13), train_t) xs = np.linspace(1, 12, 100) ys = predict(xs) subplot.plot(xs, ys) plt.savefig('ex1-2.png') #plt.show() print('\nLog of computer spec.') specrep.report()