How to create an MPLClassifier from weights and biases? (Python 3) - python

I am trying to create an MPLClassifier with predefined weights and biases so that I can save them to a file and then
If I train the network like this:
import numpy as np
from sklearn.neural_network import MLPClassifier
data = np.load("data.npy")
labels = np.load("labels.npy")
clf = MLPClassifier()
clf.fit(data, labels)
np.save("weights.npy", clf.coefs_)
np.save("biases.npy", clf.intercepts_)
and then access the weights an biases like this:
import numpy as np
from sklearn.neural_network import MLPClassifier
weights = np.load("weights.npy")
biases = np.load("biases.npy")
I want to be able to create a new network like:
clf = MLPClassifier(weights=weights, biases=biases)

As #Plagon commented, you can't create an MLPClassifier from weights and biases.
Instead, you should import pickle and use it like:
with open("network.pkl", "wb") as network:
pickle.dump(clf, network)
and access it like:
with open("network.pkl", "wb") as network:
clf = pickle.load(network)
For more information on pickle you can go its documentation at https://docs.python.org/3/library/pickle.html.

Related

shufflenetV2 implementation using non-image datasets (csv file only)

I'm trying to implement a prediction using the Cleveland dataset from kaggle.com. I want to use shufflenet to implement as below in deep learning. All the examples I have seen use image datasets. I need guidance on how to go about this using non-image datasets like the Cleveland or SAHeart heart disease datasets.
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,precision_score
from keras.models import Sequential
from keras.layers import Dense
############# main ################
if __name__ == '__main__':
lc= "heart.csv"
dataset = pd.read_csv(lc)
predictors = dataset.drop("target",axis=1)
target = dataset["target"]
X_train,X_test,Y_train,Y_test = train_test_split(predictors,target,test_size=0.20,random_state=0)
model = Sequential()
model.add(Dense(11,activation='relu',input_dim=13))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X_train,Y_train,epochs=300)
#Predict
Y_pred_nn = model.predict(X_test)
rounded = [round(x[0]) for x in Y_pred_nn]
Y_pred_nn = rounded
score_nn = round(accuracy_score(Y_pred_nn,Y_test)*100,2)
precision_score_nn = precision_score(Y_pred_nn, Y_test, average=None)
#Print Accuracy score
print("The accuracy score achieved using Neural Network is: "+str(score_nn)+" %")
print("The precision score achieved using Neural Network is: "+str(precision_score_nn)+" %")

How to add a sklearn target transformer (for output variable) to keras neural network pipeline?

I want to build a neural network using Keras on transforms of my input variables AND my output variables using the sklearn Pipeline (so I can perform CV). I am trying to use TransformedTargetRegressor, but my mean squared errors do not make sense to me.
This is my code which is adapted from Sklearn's example for TransformedTargetRegressor using the Boston Housing dataset and adding a simple neural network that scales the input variables (X).
Set up (this section is fine):
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import load_boston
from sklearn.compose import TransformedTargetRegressor
from sklearn.model_selection import train_test_split
#load data
X, y = load_boston(return_X_y=True)
#define simple neural network
def simple_nn():
model = Sequential()
model.add(Dense(13, input_dim=13, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer = 'adam')
return model
#create pipeline for input variables (X) preprocessing
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=simple_nn, epochs=100, batch_size=5, verbose=True)))
pipeline = Pipeline(estimators)
I am trying to do the following (section in question):
#Section in question
transformer = MinMaxScaler()
model = TransformedTargetRegressor(regressor=pipeline,
transformer=transformer)
results = cross_val_score(model, X, y, cv=KFold(n_splits=5))
The resulting cross validation scores are:
array([ 0.61321517, 0.35811762, -2.67674546, -0.30623006, -0.38187424])
The middle number is of particular concern to me since the y target is supposed to have been scaled from 0 to 1, so a mean squared error of -2.67 seems wrong. What am I doing wrong here?
A mean squared error is squared, and thus can't be negative.
That means that your score is not the mean squared error.
The cross_val_score documentation tells us that if not defined, the scorer default to the estimator scorer :
"If None, the estimator’s default scorer (if available) is used.
In your case, it's the TransformedTargetRegressor regressor that is being used. And the TransformedTargetRegressor documentation tells us that its default score :
Return the coefficient of determination R^2 of the prediction.
So the values your are displaying are R2 scores. It can be negative if your model perform badly. See this question for instance.
As a good practice, you should always define the scorer you want to use, to avoid relying on the wrong one.

How to view network weights and bias during training

I have the below code.
I would like to see how the weights and bias changes during training.
Ideally I would like to see it in tensorboard.
Would someone be able to show me how to do this.
from time import time
import numpy as np
import matplotlib.pyplot as plt
import keras
import tensorflow as tf
from keras.callbacks import TensorBoard
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
x = scaler.fit_transform(np.array([[1965.0], [1980.0]])).reshape(-1,1)
y = scaler.fit_transform(np.array([[320.0], [345.0]])).reshape(-1,1)
tensorboard = TensorBoard(log_dir='logs/{}'.format(time()), write_grads=True)
model = keras.Sequential([keras.layers.Dense(1, activation='linear')])
model.compile(optimizer='sgd',
loss="mean_squared_error")
model.fit(x=x, y=y, epochs=1000, callbacks=[tensorboard])
yHat = model.predict(x)
Based on the Keras documentation, all you need to do maybe is just run the command line:
tensorboard --logdir=logs
Notice that the logdir setting is pointing to the root of your log directory.

beginner question, using sklearn, saving model and test a single dataframe

New to python neural networks and sklearn, i wrote the following neural model. On a train set, it works nicely around 98% accuracy.
Now i have some questions.
from sklearn.ensemble import GradientBoostingClassifier
model = GradientBoostingClassifier(learning_rate=0.99,max_depth=3)
model.fit(X_standardized, y)
predictions = model.predict(X_standardized)
from sklearn.metrics import confusion_matrix, classification_report
print(confusion_matrix(y, predictions))
print ()
print(classification_report(y,predictions))
Can the state of the neural network be saved and loaded.
ea store the weights gradients.
#something like:
Model.save("c:\neural\testnet.xml")
How to perform individual tests on a single data frame ea:
print ("answer =" ,Model.TestSample(test_data_frame)) # single input
>>> answer = 0.78 ...estimated accuracy 97% # or so
Regarding saving the state of the model: you can save the model using pickle package, e.g.:
import pickle
pickle.dump(model, open('model.sav', 'wb'))
Not sure what you mean by 'individual tests on a single data frame', but if you want to test the model on some different (test) data, you can just create something like that:
import sklearn
df_predictions = model.predict( *input X data* )
accuracy = sklearn.metrics.r2_score(*target (y data)*, df_predictions)

Save and Restore tensor flow tf.contrib model

There are plenty of examples for how to save a tensorflow model using a tf.train.Saver object, but I was wondering how to save a model that comes from tf.contrib. For example using the code from the quickstart demo at https://www.tensorflow.org/versions/master/tutorials/tflearn/index.html:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TEST,
target_dtype=np.int,
features_dtype=np.float32)
# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")
# Fit model.
classifier.fit(x=training_set.data,
y=training_set.target,
steps=2000)
Now that I have classifier trained, how might I save the model to disk so that later on I can write a script that does:
# classifier = <some reloading code that reinitializes the trained model>
accuracy_score = classifier.evaluate(x=test_set.data,
y=test_set.target)["accuracy"]
with minimal overhead and knowledge of the previous script?

Categories

Resources