コード例 #1
0
# Change random seed to get different random numbers: seed (integer)
# Change number of training data samples: ntrain up to 60000
# Change number of validation data samples: nvalid up to 10000
# Change learning rate for optimization: learning_rate >0
# Change number of iterations: niterations
seed = 10
ntrain = 6000
nvalid = 1000
learning_rate = 0.02
niteration = 40
# (1) Set up data
nclass = 10
Xtrain, Ytrain, Xvalid, Yvalid = load_mnist.load_mnist(ntrain, nvalid)
# (2) Define model
nfeature = Xtrain.shape[0]
np.random.seed(seed)
model = NeuralNetwork.NeuralNetwork(nfeature)
model.add_layer(128, "relu")
model.add_layer(nclass, "softmax")
# (3) Compile model
optimizer = Optimizer.Adam(learning_rate, 0.9, 0.999, 1e-7)
model.compile("crossentropy", optimizer)
model.summary()
# (4) Train model
history = model.fit(Xtrain, Ytrain, niteration)
# (5) Predictions and plotting
# plot data, loss, and animation of results
Yvalid_pred = model.predict(Xvalid)
plot_results.plot_data_mnist(Xtrain, Ytrain)
plot_results.plot_results_history(history, ["loss"])
plot_results.plot_results_mnist_animation(Xvalid, Yvalid, Yvalid_pred, 25)
コード例 #2
0
# driver_logisticregression.py

import LRegression
import example_classification
import matplotlib.pyplot as plt
import numpy as np
import Optimizer
import plot_results

# (1) Set up data
nfeature = 2
m = 1000
case = "linear"
nclass = 2
X, Y = example_classification.example(nfeature, m, case, nclass)
# (2) Define model
lamb = 0.01
model = LRegression.LRegression(nfeature, "sigmoid", lamb)
# (3) Compile model
optimizer = Optimizer.GradientDescent(0.5)
model.compile("binarycrossentropy", optimizer)
# (4) Train model
epochs = 100
history = model.fit(X, Y, epochs)
# (5) Results
# plot loss and accuracy
plot_results.plot_results_history(history, ["loss"])
plot_results.plot_results_history(history, ["accuracy"])
# plot heatmap in x0-x1 plane
plot_results.plot_results_classification((X, Y), model, nclass)
plt.show()
コード例 #3
0
ntrain = 6000
nvalid = 1000
nclass = 10
Xtrain,Ytrain,Xvalid,Yvalid = load_mnist.load_mnist(ntrain,nvalid)
# (2) Define model
nfeature = Xtrain.shape[0]
np.random.seed(10)
lamb = 0.0
model = NeuralNetwork.NeuralNetwork(nfeature)
model.add_layer(128,"relu",lamb)
model.add_layer(nclass,"softmax",lamb)
# (3) Compile model
optimizer = Optimizer.Adam(0.02,0.9,0.999,1e-7)
model.compile("crossentropy",optimizer)
model.summary()
# (4) Train model
epochs = 40
time_start = time.time()
history = model.fit(Xtrain,Ytrain,epochs,batch_size=ntrain,validation_data=(Xvalid,Yvalid))
time_end = time.time()
print("Train time: {}".format(time_end - time_start))
# (5) Predictions and plotting
# confusion matrix
print("Metrics for Validation Dataset")
Yvalid_pred = model.predict(Xvalid)
metrics.confusion_matrix(Yvalid,Yvalid_pred,nclass)
# plot loss, accuracy, and animation of results
plot_results.plot_results_history(history,["loss","valid_loss"])
plot_results.plot_results_history(history,["accuracy","valid_accuracy"])
plot_results.plot_results_mnist_animation(Xvalid,Yvalid,Yvalid_pred,model.get_Afinal(),100)
plt.show()
コード例 #4
0
print("XvalidT.shape: {} - YvalidT.shape: {}".format(XvalidT.shape,
                                                     YvalidT.shape))
# (2) Define model
nfeature = Xtrain.shape[0]
lamb = 0.0001
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=1,
                          input_shape=(nfeature, ),
                          activation="linear",
                          kernel_regularizer=tf.keras.regularizers.l2(lamb))
])
# (3) Compile model
optimizer = tf.keras.optimizers.SGD(learning_rate=0.5)
model.compile(optimizer=optimizer,
              loss="mean_squared_error",
              metrics=["mean_absolute_error"])
# (4) Train model
epochs = 50
ntrain = XtrainT.shape[0]
history = model.fit(XtrainT,
                    YtrainT,
                    epochs=epochs,
                    batch_size=ntrain,
                    validation_data=(XvalidT, YvalidT))
# (5) Predictions and plotting
Yvalid_pred = model.predict(XvalidT)
# plot loss and accuracy (mean absolute error for linear regression)
plot_results.plot_results_history(history.history, ["loss", "val_loss"])
plot_results.plot_results_history(
    history.history, ["mean_absolute_error", "val_mean_absolute_error"])
plt.show()