# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Reset environment for a new run % reset -f # Load Libraries from os.path import join # for joining file pathnames import pandas as pd import tensorflow as tf from tensorflow import keras import numpy as np import matplotlib.pyplot as plt # Set Pandas display options pd.options.display.max_rows = 10 pd.options.display.float_format = '{:.1f}'.format features = np.array(range(10)) features = features[:, np.newaxis] # Create labels by adding noise distributed around 0 labels = features + np.random.random(size=[10,1]) - 0.5 # Visualize the data plt.scatter(features,labels) # Delete any existing assignment to "model" model = None # Use a sequential model model = keras.Sequential() # Add a layer with 1 neuron. Use the popular "tanh" activation function model.add(keras.layers.Dense(units=1, # 1 neuron activation='tanh', # 'tanh' input_dim=1)) # number of feature cols=1 # Model calculates loss using mean-square error (MSE) # Model trains using Adam optimizer with learning rate = 0.001 model.compile(optimizer=tf.optimizers.Adam(0.001), loss='mse', ) print(model.summary()) model.fit(x=features, y=labels, epochs=10, # train for 10 epochs batch_size=10,# use 10 examples per batch verbose=1) # verbose=1 prints progress per epoch model = None model = keras.Sequential() model.add(keras.layers.Dense(1, activation='linear', input_dim=1)) model.compile(optimizer=tf.optimizers.Adam(0.001), loss='mse') trainHistory = model.fit(features, labels, epochs=10, batch_size=1, verbose=1) # Plot loss curve plt.plot(trainHistory.history['loss']) plt.title('Loss Curves') model = None model = keras.Sequential() model.add(keras.layers.Dense(1, activation='linear', input_dim=1)) model.compile(optimizer=tf.optimizers.Adam(0.1), loss='mse') model.fit(features, labels, epochs=5, batch_size=1, verbose=1) # get predictions featuresPred = model.predict(features, verbose=1) # Plot original features and predicted values featuresPred = np.transpose(featuresPred) plt.scatter(range(10), labels, c="blue") plt.scatter(range(10), featuresPred, c="red") plt.legend(["Original", "Predicted"]) # create data with large values features = np.array(range(50)) # generate labels labels = features + np.random.random(features.shape) - 0.5 # Transpose data for input [features, labels] = [features.transpose(), labels.transpose()] plt.scatter(range(len(features)), features) # Train on raw data model = None model = keras.Sequential() model.add(keras.layers.Dense(1, input_dim=1, activation='linear')) model.compile(optimizer=keras.optimizers.SGD(0.01), loss='mse') model.fit(features, labels, epochs=5, batch_size=10, verbose=1)