Check the following code:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Flatten
from sklearn.model_selection import train_test_split
# Data
X = np.random.rand(1000, 100, 1)
y = np.random.randint(0, 2, (1000, 1))
# Splitting into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Conv1D
model = Sequential()
model.add(Conv1D(32, kernel_size=3, activation='relu', input_shape=(100, 1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
# Predict before fitting the model
cnn_features_train = model.predict(X_train)
cnn_features_test = model.predict(X_test)
Why this runs without throwing an error? The weights are not yet stabilished by the .fit method, how can it predict something?
If i try to do the same thing (predict before fitting the model) using Sklearn i get the expected error, for example:
from sklearn.ensemble import RandomForestClassifier
# Data
X = np.random.rand(1000, 100, 1)
y = np.random.randint(0, 2, (1000, 1))
# Splitting into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Random Forest
rf = RandomForestClassifier()
rf.predict(X_test)
The error:
sklearn.exceptions.NotFittedError: This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
Keras is different from sklearn. The .predict ()without calling .fit() helps users in preparing and debugging the correct shapes of the tensor.
Related
I am working on my first neural network, and i'm stuck on one error. Here is the code:
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv('iris.csv')
X = pd.get_dummies(df.drop(['variety'], axis=1))
y = df['variety'].apply(lambda x: 0 if x=='Setosa' else (1 if x=='Versicolor' else 2))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
print(y_train.head())
from keras.models import Sequential, load_model
from keras.layers import Dense
from sklearn.metrics import accuracy_score
model = Sequential()
model.add(Dense(units=8, activation='relu', input_dim=len(X_train.columns)))
model.add(Dense(units=3, activation='sigmoid'))
model.add(flatten())
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy')
model.fit(X_train, y_train, epochs=50, batch_size=1)
I am working off of a tutorial on tensorflow, and am using https://www.kaggle.com/datasets/arshid/iris-flower-dataset as the dataset to train on. I used the code from the tutorial, but changed it to fit my dataset. Still, I get the ValueError. Any help?
import pandas as pd
import numpy as np
import tensorflow as tf
data = pd.read_csv("Amex.csv")
data.head()
X = data.iloc[:, :-1].values
Y = data.iloc[:, -1].values
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=1234)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=1000, activation='sigmoid'))
ann.add(tf.keras.layers.Dense(units=1280, activation='sigmoid'))
ann.add(tf.keras.layers.Dense(units=10, activation='softmax'))
ann.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
ann.fit(x_train, y_train, batch_size=32, epochs=200)
print(ann.predict(sc.transform([[3,7,9,8,8,1,4,4,7,0,4,5,2,6]])))`
I have trained the model with an accuracy of 0.9994 The answer should be 1, but I get an array list
output
[[8.7985291e-06 2.5825528e-04 2.8821041e-03 1.0145088e-04 1.5824498e-04 8.1912667e-06 1.9685100e-03 9.9447292e-01 6.3032545e-05 7.8425743e-05]]
Thanks #Dr. Snoopy for the answer and #AlphaTK for confirming that the issue got resolved. Adding this comment into the answer section for the community benefit.
This is just an array of probabilities output by the model and an
argmax should be applied to obtain a class index.
as you can see below i try to create an MLP with tensorflow/keras. But unfortunately the loss is always NaN when fitting. Do you have any advice?
as a second error message i get the message "'Functional' object has no attribute 'score'" when trying to measure accuracy with model.score, but i think this is a problem that is triggered by the first one.
thanks to all
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from mpl_toolkits import mplot3d
from sklearn import datasets
from various import printShapes, printNumpy, print_Model_Accuracy, printLARGE, checkFormat
from sklearn.datasets import make_blobs
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
np.random.seed(1234)
#%matplotlib qt
#%matplotlib inline
plt.rcParams["figure.figsize"] = [4*2, 4*2]
if 0:
iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.80, random_state=1234)
if 1:
X, y = make_blobs(n_features=4, centers=3, n_samples=1000, cluster_std = 5.0, random_state=1234)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1234)
print ("Target Label Example: y_train[0]:")
print (y_train[0])
print (type(y_train[0]))
printLARGE("MLP classifier TENSORFLOW")
tf.random.set_seed(1234)
Epochs = 10
inputs = keras.Input(shape=(4,), name="digits")
x = layers.Dense(100, activation="tanh", name="dense_1")(inputs)
x = layers.Dense(4, activation="tanh", name="dense_2")(x)
outputs = layers.Dense(3, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(
optimizer=keras.optimizers.RMSprop(), # Optimizer
loss=keras.losses.SparseCategoricalCrossentropy(), # Loss function to minimize
metrics=[keras.metrics.SparseCategoricalAccuracy()], # List of metrics to monitor
)
printShapes(X_train, "X_train", y_train, "y_train")
# TRAINING
model.fit(X_train, y_train, batch_size=64, epochs=Epochs)
printShapes(X_test, "X_test", y_test, "y_test")
# INFERENCE
y_test_predproba = model.predict(X_test)
print(y_test_predproba)
y_test_pred = np.argmax(y_test_predproba, axis = 1)
print(y_test_pred)
print_Model_Accuracy(model, X_test, y_test, y_test_pred)
Using tanh activation function in the hidden layers does not make
any sense. It should be ReLU.
Using one more hidden layer will be better than using more units in the first layer. [for your task]
However, using more hidden layers makes the model more vulnerable to over-fitting, adding Dropout layers solves the issue.
Finally, your model should be,
inputs = keras.Input(shape=(4,), name="digits")
x = layers.Dense(32, activation="relu", name="dense_1")(inputs)
x = layers.Dropout(0.2)(x)
x = layers.Dense(24, activation="relu", name="dense_2")(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(16, activation="relu", name="dense_2")(x)
outputs = layers.Dense(3, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
I am training a neural network with a simple dataset. I have tried different combinations of parameters, optimizers, learning rates ... but even after 20 epochs the network is still not learning anything.
I wonder where in the following code lies the problem?
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Input, Dense, Flatten
from tensorflow import keras
from livelossplot import PlotLossesKeras
from keras.models import Model
from sklearn.datasets import make_classification
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd
seed = 42
X, y = make_classification(n_samples=100000, n_features=2, n_redundant=0,
n_informative=2, random_state=seed)
print(f"Number of features: {X.shape[1]}")
print(f"Number of samples: {X.shape[0]}")
df = pd.DataFrame(np.concatenate((X,y.reshape(-1,1)), axis=1))
df.set_axis([*df.columns[:-1], 'Class'], axis=1, inplace=True)
df['Class'] = df['Class'].astype('int')
X = df.drop('Class', axis=1)
y = df['Class']
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
print(f"Train set: {X_train.shape}")
print(f"Validation set: {X_val.shape}")
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.astype(np.float64))
X_val_scaled = scaler.transform(X_val.astype(np.float64))
inputs = Input(shape=X_train_scaled.shape[1:])
h0 = Dense(5, activation='relu')(inputs)
h1 = Dense(5, activation='relu')(h0)
preds = Dense(1, activation = 'sigmoid')(h1)
model = Model(inputs=inputs, outputs=preds)
opt = keras.optimizers.Adam(lr=0.0001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(X_train_scaled, y_train, batch_size=128, epochs=20, verbose=0,
validation_data=(X_val_scaled, y_val),
callbacks=[PlotLossesKeras()])
score_train = model.evaluate(X_train_scaled, y_train, verbose=0)
score_test = model.evaluate(X_val_scaled, y_val, verbose=0)
print('Train score:', score_train[0])
print('Train accuracy:', score_train[1])
print('Test score:', score_test[0])
print('Test accuracy:', score_test[1])
The code produces the following kind of output
You have used wrong loss function, change this line
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
to, for example,
model.compile(optimizer=opt, loss='mse', metrics=['accuracy'])
Categorical cross-entropy needs a one-hot encoded y which means, you have to have a 0 or a 1 for every class. MSE is just mean squared error, so it will work. But you might try some other losses as well.
your y:
[1,0,1]
one-hot encoded y:
[[0,1], [1,0], [0,1]]
I am working on a specific neural network which gets two different inputs:
the MNIST data set, the train set is a [50000,784] tensor
an auxiliary vector with the TensorShape([Dimension(28)])]
When I define and run the model it as to below
from tensorflow.examples.tutorials.mnist import input_data
from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras.objectives import binary_crossentropy
from keras.callbacks import LearningRateScheduler
import numpy as np
import keras
import matplotlib.pyplot as plt
import keras.backend as K
import tensorflow as tf
from keras.callbacks import LambdaCallback
def load_dataset(flatten=False):
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
# normalize x
X_train = X_train.astype(float) / 255.
X_test = X_test.astype(float) / 255.
# we reserve the last 10000 training examples for validation
X_train, X_val = X_train[:-10000], X_train[-10000:]
y_train, y_val = y_train[:-10000], y_train[-10000:]
if flatten:
X_train = X_train.reshape([X_train.shape[0], -1])
X_val = X_val.reshape([X_val.shape[0], -1])
X_test = X_test.reshape([X_test.shape[0], -1])
return X_train, y_train, X_val, y_val, X_test, y_test
X_train, y_train, X_val, y_val, X_test, y_test = load_dataset(True)
original_dim=784
m = 100 #batchsize
n_z =8
n_epoch = 10
n_d =int(n_z*(n_z - 1 )/2) #or n_d=28
A_vec = K.random_normal(shape=(n_d,), mean=0., stddev=1.)
image_inputs = Input(shape=(784,))
A_inputs = Input(shape=(n_d,))
inputs = keras.layers.concatenate([image_inputs, A_inputs])
h_q1 = Dense(512, activation='relu')(inputs)
h_q2 = Dense(256, activation='relu')(h_q1)
h_q3 = Dense(128, activation='relu')(h_q2)
h_q4= Dense(64, activation='relu')(h_q3)
mu = Dense(n_z, activation='linear')(h_q4)
log_sigma = Dense(n_z, activation='linear')(h_q4)
............
After running the model,
vae.fit([X_train,A_vec], outputs,shuffle=True, batch_size=m, epochs=n_epoch)
I get this error:
ValueError: All input arrays (x) should have the same number of
samples. Got array shapes: [(50000, 784),
TensorShape([Dimension(28)])]
It means my inputs have different sizes. How can I use differetn inputs when they have different sizes (or shapes)?
The inputs have to have the same size, e.g. (50000, 748) and (50000, 28), i.e. one per sample. Try create a numpy array size (50000, 28) for A_vec: numpy.random.normal(0., 1.0, (50000, 28).
Or if you want the same vector for all, create it and repeat 50000 times.