I am using the sequential model in Keras. I would like to check the weight of the model after every epoch. Could you please guide me on how to do so.
model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=5 validation_data=(X_test, y_test))
Thanks in advance.
What you are looking for is a CallBack function. A callback is a Keras function which is called repetitively during the training at key points. It can be after a batch, an epoch or the whole training. See here for doc and the list of callbacks existing.
What you want is a custom CallBack that can be created with a LambdaCallBack object.
from keras.callbacks import LambdaCallback
model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
print_weights = LambdaCallback(on_epoch_end=lambda batch, logs: print(model.layers[0].get_weights()))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy'])
model.fit(X_train,
y_train,
batch_size=batch_size,
nb_epoch=5 validation_data=(X_test, y_test),
callbacks = [print_weights])
the code above should print your embedding weights model.layers[0].get_weights() at the end of every epoch. Up to you to print it where you want to make it readable, to dump it into a pickle file,...
Hope this helps
Related
I want to use some Light gbm functions properly.
This is standard approach, it's no different than any other classifier from sklearn:
define X, y
train_test_split
create classifier
fit on train
predict on test
compare
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
#here maybe DecisionTreeClassifier(), RandomForestClassifier() etc
model = lgb.LGBMClassifier()
model.fit(X_train, y_train)
predicted_y = model.predict(X_test)
print(metrics.classification_report())
but light gbm has its own functions like lgb.Dataset, Booster.
However, in this kaggle notebook, it's not calling LightGBMClassifier at all!
Why?
what is the standard order to call lgbm functions and train models the 'lgbm' way?
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
#why need this Dataset wrapper around x_train,y_train?
d_train = lgbm.Dataset(X_train, y_train)
#where is light gbm classifier()?
bst = lgbm.train(params, d_train, 50, early_stopping_rounds=100)
preds = bst.predict(y_test)
why does it train right away?
LightGBM has a few different API with different names of the methods (LGBMClassifier, Booster, train, etc.), parameters, and sometimes different types of data, that is why train method does not need to call LGBMClassifier but needs another type of dataset. There is no right/wrong/standard way - all of them are good if well used.
https://lightgbm.readthedocs.io/en/latest/Python-API.html#training-api
I use AdaBoostClassifier from sklearn.ensemble. I trained my model using 1000 estimators:
model = AdaBoostClassifier(
base_estimator = DecisionTreeClassifier(max_depth = 6),
n_estimators = 1000,
learning_rate = 0.2
)
model.fit(X_train, y_train)
then using generator model.staged_predict_proba(X_test) I get know that the best accuracy for X_test data is for 814 estimators.
Now I don't want to use generator model.staged_predict_proba(X_test) to make prediction for new test data X_test_2 because it is a lot of time to calculate predictions for each number of estimators (the dataset is really big). I just want to calculate predictions for model based on 814 estimators. I didn't find a way to do it. Is it possible for AdaBoostClassifier? I think it should be.
I am implementing a simple NN on wine data set. The NN works well and produces the prediction score, however, when I am trying to explore the actual predicted values on the test data set, I receive an array with dtype=float32 values, as oppose to values of the classes.
The classes are labelled as 1, 2, 3
I have 13 attributes and 178 observations (small data set)
Below is the the code on the implementation and the outcome I get:
df.head()
enter image description here
X=df.ix[:,1:13]
y= np.ravel(df.Type)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
scale the data:
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
define the NN
model = Sequential()
model.add(Dense(13, activation='relu', input_shape=(12,)))
model.add(Dense(4, activation='softmax'))
fit the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train1,epochs=20, batch_size=1, verbose=1)
Now this is where I store my predictions into y_pred and get the final score:
`y_pred = model.predict(X_test)`
`score = model.evaluate(X_test, y_test1,verbose=1)`
`59/59 [==============================] - 0s 2ms/step
[0.1106848283591917, 0.94915255247536356]`
When i explore y_pred I see the following:
`y_pred[:5]`
`array([[ 3.86571424e-04, 9.97601926e-01, 1.96467945e-03,
4.67598657e-05],
[ 2.67244829e-03, 9.87006545e-01, 7.04612210e-03,
3.27492505e-03],
[ 9.50196641e-04, 1.42343721e-04, 4.57215495e-02,
9.53185916e-01],
[ 9.03929677e-03, 9.63497698e-01, 2.62350030e-02,
1.22799736e-03],
[ 1.39460826e-05, 3.24015366e-03, 9.96408522e-01,
3.37353966e-04]], dtype=float32)`
Not sure why I do not see the actual predicted classes as 1,2,3?
After trying to convert into int I just get an array of zeros, as all values are so small.
Really appreciate your help!!
You are seeing the probabilities for each class. To convert probabilities to class just take the max of each case.
import numpy as np
y_pred_class = np.argmax(y_pred,axis=1)
I'm making my first steps on keras and I'm trying to do binary classification on the cancer dataset available in scikit-learn
# load dataset
from sklearn import datasets
cancer = datasets.load_breast_cancer()
cancer.data
# dataset into pd.dataframe
import pandas as pd
donnee = pd.concat([pd.DataFrame(data = cancer.data, columns = cancer.feature_names),
pd.DataFrame(data = cancer.target, columns = ["target"])
], axis = 1)
# train/test split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(donnee.loc[:, donnee.columns != "target"], donnee.target, test_size = 0.25, random_state = 1)
I'm trying to follow keras' tutorial here : https://keras.io/#getting-started-30-seconds-to-keras
The thing is, I always get the same loss value (6.1316862406430541), and the same accuracy (0.61538461830232527), because the predictions are always 1.
I'm not sure if it's because of a code error :
I don't know, maybe the shape of X_train is wrong ?
Or maybe I'm doing something wrong with epochs and/or batch_size.
Or if it's because of the network itself :
if I'm not mistaken, all 1 predictions is possible if there's no biases to the layers, and I don't know yet how they're initialized
But maybe it's something else, maybe 1 layer only is too few ? (if so, I wonder why keras' tutorial is 1 layer only...)
Here is my code, if you have any idea :
import keras
from keras.models import Sequential
model = Sequential()
from keras.layers import Dense
model.add(Dense(units=64, activation='relu', input_dim=30))
model.add(Dense(units=1, activation='sigmoid'))
model.summary()
model.compile(loss = keras.losses.binary_crossentropy,
optimizer = 'rmsprop',
metrics=['accuracy']
)
model.fit(X_train.as_matrix(), y_train.as_matrix().reshape(426, -1), epochs=5, batch_size=32)
loss_and_metrics = model.evaluate(X_test.as_matrix(), y_test.as_matrix(), batch_size=128)
loss_and_metrics
classes = model.predict(X_test.as_matrix(), batch_size=128)
classes
This is a very usual case. If you check the histogram of your data you will see that there are data points in your dataset which coordinates spans from 0 to 100. When you feed such data to neural network input to sigmoid might be so big that it will suffer from underflow. In order to scale data, you could use either MinMaxScaler or StandardScaler thanks to what you'll make your data to have a span suitable for neural network computations.
How to fit data from InceptionV3 to ImageDataGenerator?
The examples I found for fitting data to ImageDataGenerator are for mnist or cifar10, like this:
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# fit parameters from data
datagen.fit(X_train)
But can I fit data for InceptionV3 model to my ImageDataGenerator?
I load my Inception V3 model like:
base_model = InceptionV3(weights='imagenet', include_top=True)
datagen = ImageDataGenerator(...)
datagen.fit(base_model.get_layer('avg_pool').output)
But I get error saying 'ValueError: setting an array element with a sequence.'
I assume that you need to do this in two steps. First feed in your data into the InceptionV3 model and saving the output into a numpy array. Then feeding this numpy array into your second model.
First step like so (taken from here):
generator = datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=batch_size,
class_mode=None, # this means our generator will only yield batches of data, no labels
shuffle=False) # our data will be in order
bottleneck_features_train = model.predict_generator(generator, 2000)
np.save(open('bottleneck_features_train.npy', 'w'),
bottleneck_features_train)