My kernel died whenever I run model.predict_classes() - python
I'm using macbook pro m1 and found out that I can't use tensorflow with Anaconda so I installed it step by step by the following link:
https://towardsdatascience.com/installing-tensorflow-on-the-m1-mac-410bb36b776
I can import tensorflow now and tested with the code in the following link and got a problem. https://machinelearningmastery.com/neural-network-for-cancer-survival-dataset/
It runs successfully on colab but not on my macbook.
Here are the codes:
# fit a simple mlp model on the haberman and review learning curves
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/haberman.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, stratify=y, random_state=3)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit the model
history = model.fit(X_train, y_train, epochs=200, batch_size=16, verbose=0, validation_data=(X_test,y_test))
When I run this one:
# predict test set
yhat = model.predict_classes(X_test)
the kernel died.
I've tried to delete miniforge3 folder and do the tensorflow installation again but the problem still exists.
Versions:
Python 3.8.10
tensorflow 2.4.0-rc0
There are some WARNING coming up but I don't think that matters, if it may, please ask me to post it up here.
I met the same problem, when label y is a sparse number in multi-class task. After I transform label y into one-hot vector, the problem just disappears. However, I didn't met this problem in binary-class problem. Maybe do some preprocessing on labels.
Related
How to change a negative r2_score result from Keras code
So I am trying to develop a deep learning program, which can predict the quality of wine based as a regression problem. The dataset is from https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/. I have read some tutorials but it is mainly based on https://www.datacamp.com/community/tutorials/deep-learning-python. This code is written and run on colab.research.google.com. It runs without any issues, however the r2_score is negative and I don't fully comprehend why we are using sometimes X_test and X[test] e.g. for predicting the r2_score etc. import matplotlib.pyplot as plt import h5py # export models in HDF5 format from keras.datasets import mnist from keras.utils import np_utils from keras.layers import Activation, Dense, Dropout from keras.models import Sequential from keras import optimizers from keras import losses from keras import metrics from sklearn.preprocessing import StandardScaler from sklearn.model_selection import StratifiedKFold from sklearn.model_selection import train_test_split import numpy as np import pandas as pd # Read in red wine data # Read a comma-separated values (csv) file into DataFrame. red = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=';') X = red.drop('quality', axis=1) # Isolate data # Drop specified labels from rows or columns. # same as ix[:,0:11] Y = red.quality X = StandardScaler().fit_transform(X) # Scale the data with `StandardScaler # StandardScaler transforms data such that its distribution will have a mean value 0 and standard deviation of 1. # Each value in the dataset will have the sample mean value subtracted, and then divided by the standard deviation of the whole dataset. X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42) seed = 7 np.random.seed(seed) kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed) NB_EPOCH = 20 # split up the data into K partitions / K-fold cross-validation # Generate indices to split data into training and test set for train, test in kfold.split(X, Y): model = Sequential() # Initialize the model model.add(Dense(64, input_dim=11, activation='relu')) # Add input layer model.add(Dense(1)) # Add output layer model.compile(optimizer='rmsprop', loss='mse', metrics=['mae']) #model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(), metrics=['accuracy']) history = model.fit(X[test], Y[test], validation_split=0.25, epochs=NB_EPOCH, verbose=1) #history = model.fit(X_train, y_train, validation_split=0.25, epochs=20, verbose=1) mse_value, mae_value = model.evaluate(X[test], Y[test], verbose=0) print("Mean Squared Error: "+ str(mse_value)) # quantifies the difference between the estimator and what is estimated print("Mean Absolute Error: " + str(mae_value)) #quantifies how close predictions are to the eventual outcomes score = model.evaluate(X_test, y_test, verbose=1) print("Test score:", score[0]) print('Test accuracy:', score[1]) # generating the graph through matplotlib # Plot training & validation mea values fig= plt.figure(figsize=(20,5)) plt.plot(history.history['mean_absolute_error']) plt.plot(history.history['val_mean_absolute_error']) plt.title('Model MAE') plt.ylabel('MAE') plt.xlabel('Epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() from sklearn.metrics import r2_score y_pred = model.predict(X[test]) print("this is r2:" + str(r2_score(Y[test], y_pred))) print(test) print(X[test])
reason for splitting data to train and test is really simple if you train your model with all of your data, your model would work better for your data ( because of seeing it before) but when you want to see how it work on new data it won't work well (doesn't generalize) so you should put a part of your data away so you could test your model, see if it generalize well. and when you want to what is the prediction of a new set to see how your model works you could use x[test]. and when you are calculating your error (r2) it is (y[test]-y_pred) for each sample so it could be negative or positive ( you are checking the difference of what you have predicted and what you should have got so a negative r2 means you are predicting lower than mean value) you could use mse for check it too (it's better for regression problems) something like this: MSE = np.square(np.subtract(Y_true,Y_pred)).mean()
Keras estimator predict says that input is misshapen
I'm getting a numpy shape error when I use the predict function of a Keras estimator. I build, evaluate, and then retrain the model using the following code: import pandas as pd import sqlalchemy as sqla import numpy from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier from keras.utils import np_utils from keras.utils.np_utils import to_categorical from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold from sklearn.preprocessing import LabelEncoder from sklearn.pipeline import Pipeline # Connect to to the DB and retrieve the iris table con = sqla.create_engine('postgresql://tristan:sebens#db:5432/tristan') con.connect() table_name = "iris" schema = "public" iris = pd.read_sql_table(table_name, con, schema=schema) iris.head() iris_ds = iris.values # Convert the table to a numpy array X = iris_ds[:, 0:4].astype(float) # Slice the descriptive features into a numpy array Y = iris_ds[:, 4] # Slice the labels away as their own numpy array # The labels are encoded as strings, so we need to encode them # as numbers that can be output by an ANN encoder = LabelEncoder() encoder.fit(Y) encoded_Y = encoder.transform(Y) # convert integers to dummy variables (i.e. one hot encoded) dummy_y = to_categorical(encoded_Y) # define baseline model def baseline_model(): # create model model = Sequential() model.add(Dense(8, input_dim=4, activation='relu')) model.add(Dense(3, activation='softmax')) # Compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model seed = 7 # Train the model: # First we define the model as a classifier. This will affect the process used to train it estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0) # Honestly not totally sure what this is, but it has to do with splitting the training/evaluation data in # a way that gives us a more realistic metric of the model's accuracy kfold = KFold(n_splits=10, shuffle=True, random_state=seed) # Now that we have our classifier and our data pipeline defined, we can begin the training process results = cross_val_score(estimator, X, dummy_y, cv=kfold) print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100)) # If we like our accuracy, then we can train the model for real # Evaluating the model actually evaluates a clone of the model, so now we need to train the model again estimator.fit(X, dummy_y) And this is where the trouble is. I try to make a test prediction: # Let's make a test prediction with our model x = X[0] estimator.predict(x) And I get an input shape error: ValueError: Error when checking input: expected dense_21_input to have shape (4,) but got array with shape (1,) I'm at a loss. How can the input have the wrong shape if it's literally a member of the training dataset?
Keras multi-class classification process taking a long time
I started learning how to use Keras. I have a raw file that encodes ASCII values of characters in a sentence with a corresponding product name. For example, abcd toothpaste cream would be classified as Toothpaste. The first two lines (out of ~150,000 lines) of the code is shown below. The file is also available for download here (this link will last two months from today). 12,15,11,31,30,15,0,26,28,15,29,30,19,17,15,0,19,24,30,15,28,24,11,30,0,18,19,17,19,15,24,15,0,35,0,12,15,22,22,15,36,11,0,12,15,22,22,15,36,11,0,16,28,11,17,11,24,13,19,11,29,0,16,15,23,15,24,19,24,11,29,0,11,36,36,15,14,19,24,15,0,11,36,36,15,14,19,24,15,11,22,11,19,11,0,26,15,28,16,31,23,15,0,16,15,23,15,24,19,24,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Body Care Other 12,15,19,15,28,29,14,25,28,16,0,30,18,11,19,22,11,24,14,0,13,25,0,22,30,14,0,29,21,19,24,13,11,28,15,0,26,28,15,26,11,28,11,30,19,25,24,29,0,16,11,13,19,11,22,0,13,22,15,11,24,29,15,28,29,0,24,19,32,15,11,0,16,11,13,19,11,22,0,13,22,15,11,24,29,15,28,29,0,26,28,25,14,31,13,30,29,0,24,19,32,15,11,0,23,11,21,15,0,31,26,0,13,22,15,11,28,0,23,19,13,15,22,22,11,28,0,33,11,30,15,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Skin Care Other I am following a blog post where it uses a simple deep learning Keras model to do multi-class classification. I changed the configuration of the neural network to 243 inputs --> [100 hidden nodes] --> 67 outputs (because I have 67 classes to classify). The code is below: import numpy import pandas from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier from keras.utils import np_utils from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold from sklearn.preprocessing import LabelEncoder def baseline_model(): model = Sequential() # I changed it to 243 inputs --> [100 hidden nodes] --> 67 outputs (because I have 67 classes to classify) model.add(Dense(100, input_dim=X_len, activation='relu')) model.add(Dense(Y_cnt, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model seed = 7 numpy.random.seed(seed) # load dataset dataframe = pandas.read_csv("./input/raw_mappings.csv", header=None) dataset = dataframe.values X_len = len(dataset[0,:-1]) X = dataset[:,0:X_len].astype(float) Y = dataset[:,X_len] Y_cnt = len(numpy.unique(Y)) # encode class values as integers encoder = LabelEncoder() encoder.fit(Y) encoded_Y = encoder.transform(Y) # convert integers to dummy variables (i.e. one hot encoded) dummy_y = np_utils.to_categorical(encoded_Y) estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0) kfold = KFold(n_splits=10, shuffle=True, random_state=seed) results = cross_val_score(estimator, X, dummy_y, cv=kfold) print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100)) But it never seems to finish when I ran it on my desktop computer for more than 12 hours. I'm starting to think there is almost nothing going on. Is there something that I'm doing wrong with either the configuration of the neural network or the problem I'm trying to solve (meaning, maybe Sequential model is not the right way to go for classifying >60 classes?). Any pointer or tip would be greatly appreciated. Thank you.
Gender Voice with Python coding
I am working on a sample data set from a link below. https://www.kaggle.com/enirtium/gender-voice/data I am trying to open .csv file(maybe I am opening it wrongly) and trying to create fully connected neural layers. Then, I am trying to train them but unfortunately, I am getting input shape not fitting problem. "ValueError: Error when checking input: expected dense_1_input to have shape (None, 2800) but got array with shape (3168, 1)" My codes like these: import csv import numpy import string from keras.models import Sequential from sklearn.model_selection import train_test_split import numpy as np from keras import models from keras import layers path = r'/Users/username/Desktop/voice.csv' meanfreq = [] sd = [] median = [] label = [] with open(path, 'r') as csv_file: csv_reader = csv.reader(csv_file) next(csv_reader) for line in csv_reader: #print(line['meanfreq']) meanfreq.append(line[0]) sd.append(line[1]) median.append(line[2]) if line[20] == "female": label.append(1) else: label.append(0) network = models.Sequential() network.add(layers.Dense(512, activation='relu', input_shape=(2800,))) network.add(layers.Dense(1, activation='sigmoid')) network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) network.fit(meanfreq, label, epochs=5, batch_size=128) scores = network.evaluate(meanfreq, label) print("\n%s: %.2f%%" % (network.metrics_names[1], scores[1]*100)) I suppose that maybe, I can't open .csv file (it is opening "list" primitive) or there are any other problems. I am unfortunately fresh man at neural networks and python. I will open this csv file and will use its %70 data to train, %30 data for testing.
Yes, It works as these; import pandas as pd import numpy as np from sklearn.preprocessing import OneHotEncoder from sklearn.model_selection import train_test_split # get data ready data = pd.read_csv('voice.csv') data.shape # split out features and label X = data.iloc[:, :-1].values y = data.iloc[:, -1] # map category to binary y = np.where(y == 'male', 1, 0) enc = OneHotEncoder() # reshape y to be column vector y_ = enc.fit_transform(y.reshape(-1, 1)).toarray() X_train, X_test, y_train, y_test = train_test_split( X, y_, train_size=0.80, random_state=42) network = models.Sequential() network.add(layers.Dense(512, activation='relu', input_shape=(20,))) network.add(layers.Dense(2, activation='sigmoid')) network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) network.fit(X_train, y_train, epochs=100, batch_size=128) network.evaluate(X_test, y_test)
Reading in the data seems to be fine. I Imagine you have a data set that looks like: mean_freq, label .12 0 .45 1 And you want to train a classifier. Currently the model is expecting a training example to have 2800 features. input shape=(2800,) but you only want 1 feature: the mean_freq The mistake here is that you are trying to tell Keras how much training examples to use while declaring the model. You don't do that here, you'll do that later when you're fitting the model. So the input_shape to keras's Dense Layer should be (1, ) for the single feature. If you're going to use mean and median freq then you would want two features (2, ) and so on. # note change from 2800 to 1 network.add(layers.Dense(512, activation='relu', input_shape=(1,))) And you can split your training and test sets in multiple ways. My suggestion is to do something like this: train_size = 2800 X_train = mean_freq[:train_size] y_train = label[:train_size] X_test = mean_freq[train_size:] y_test = label[:train_size] Then fit the model with the training set and score with the test set. network.fit(X_train, y_train, epochs=5, batch_size=128) scores = network.evaluate(X_test, y_test) Edit to reflect comments: well if the case is that you training data has 20 features then you tell keras that with: # note change from 2800 to 1 network.add(layers.Dense(512, activation='relu', input_shape=(20,))) You have do the work necessary to get the data in to the shape you need for training and testing but the template above is how you would fit and evaluate the model. I would also note that there are better ways read in csv data if you're going to do modeling (as you are). Look at using a pandas dataframe. Also better (more standard ways) of creating train and test split: look into sklearn's train_test_split Edit 2: A quick model of the voice data import pandas as pd import numpy as np from sklearn.preprocessing import OneHotEncoder from sklearn.model_selection import train_test_split from keras.model import Model from keras.layers import Dense, Input # get data ready data = pd.read_csv('voice.csv') data.shape # split out features and label X = data.iloc[:, :-1].values y = data.iloc[:, -1] # map category to binary y = np.where(y == 'male', 1, 0) enc = OneHotEncoder() # reshape y to be column vector y_ = enc.fit_transform(y.reshape(-1, 1)).toarray() X_train, X_test, y_train, y_test = train_test_split( X, y_, train_size=0.80, random_state=42) # model using keras functional style inp = Input(shape =(20, )) dense = Dense(128)(inp) out = Dense(2, activation='sigmoid')(dense) model = Model(inputs=[inp], outputs=[out]) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, y_train, epochs=100, batch_size=128) model.evaluate(X_test, y_test)
LSTM implementation in keras Using specific dataset
I am trying to understand how LSTM RNNs work and how they can be implemented in Keras in order to be able to solve a binary classification problem. My code and the dataset i use are visible below. When i compilr the code i get an error TypeError: __init__() got multiple values for keyword argument 'input_dim', Can anybody help? from keras.models import Sequential from keras.layers import LSTM from keras.layers.embeddings import Embedding from keras.layers import Dense from sklearn.cross_validation import train_test_split import numpy from sklearn.preprocessing import StandardScaler # data normalization seed = 7 numpy.random.seed(seed) dataset = numpy.loadtxt("sorted output.csv", delimiter=",") X = dataset[:,0:4] scaler = StandardScaler(copy=True, with_mean=True, with_std=True ) #data normalization X = scaler.fit_transform(X) #data normalization Y = dataset[:4] # split into 67% for train and 33% for test X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=seed) # create model model = Sequential() model.add(Embedding(12,input_dim=4,init='uniform',activation='relu')) model.add(Dense(4, init='uniform', activation='relu')) model.add(LSTM(100)) model.add(Dense(1, init='uniform', activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X_train, y_train, validation_data=(X_test,y_test), nb_epoch=150, batch_size=10)
Looks like two separate questions here. Regarding how to use LSTMs / Keras, there are some good tutorials around. Try this one which also describes a binary classification problem. If you have a specific issue or area that you don't understand, let me know. Regarding the file opening issue, perhaps the whitespace in the filename is causing an issue. Check out this answer to see if it helps.
This is in fact a case where the error message you are getting is perfectly to-the-point. (I wish this would always be the case with Python and Keras...) Keras' Embedding layer constructor has this signature: keras.layers.embeddings.Embedding(input_dim, output_dim, ...) However, you are constructing it using: Embedding(12,input_dim=4,...) So figure out which is the input and output dimension, respectively, and fix your parameter order and names. Based on the table you included in the question, I'm guessing 4 is your input dimension and 12 is your output dimension; then it'd be Embedding(input_dim=4, output_dim=12, ...).