firstly im quiet new with this library. My code is simple there is input x and there is y(x*2). it should learsn simple int*2 but it cant. I think maybe paramaters are wrong but how can i determine true parameters?
from tensorflow import keras
import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
import keras
from keras.layers import Input, Dense
import numpy as np
import pandas as pd
xs = np.array([[1,3,4,5] , [9,2,3,4]]).reshape(1,2,4)
ys = np.array([[2,6,8,10] , [18,4,6,8]]).reshape(1,2,4)
# model = tf.keras.Sequential([layers.Dense(units=1, input_shape=[2,4])])
model = Sequential()
model.add(Dense(8,input_shape=[2,4]))
model.add(Activation('relu'))
model.add(Dense(6))
model.add(Activation('relu'))
model.add(Dense(4))
model.add(Activation('softmax'))
model.compile(optimizer='Adadelta', loss='mean_squared_error')
model.fit(xs, ys, epochs=5, batch_size=1)
p = np.array([[1,3,4,5] , [9,2,3,4]]).reshape(1,2,4)
print(model.predict(p))
My second try is more hard than *2 but i created 100 piece input and output value but still bad performance, how can i make it MORE accurate?
from tensorflow import keras
import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
import keras
from keras.layers import Input, Dense
import numpy as np
import pandas as pd
xs = np.array([[1,3,4,5,9,2,3,4]]).reshape(1,1,8)
xg=np.ones((100,8)).reshape(100,8)
yg=np.ones((100,8)).reshape(100,8)
for i in range(100):
xg[i-1]=xs*np.random.randint(500)
yg[i-1]=xg[i-1]*np.sin(20)
xs=xg
ys=yg
# model = tf.keras.Sequential([layers.Dense(units=1, input_shape=[2,4])])
model = Sequential()
model.add(Dense(8,input_shape=[100,8]))
model.add(Activation('relu'))
model.add(Dense(8))
model.add(Activation('relu'))
model.add(Dense(8))
# opt = keras.optimizers.sgd(learning_rate=0.2)
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(xs, ys, epochs=8000, batch_size=100)
model.summary()
p = np.array([[1,340,4,512,9,2,3,4]])
print(model.predict(p))
print(np.sin(p))
Your problem come from 2 places. First, you accidentally use softmax as output activation function, you can solve that by just comment it out (this is your root problem). Softmax function use for classification problem but your problem is regression problem there is no need for activation in the last layer. Second(after taking softmax out), it come from low number of epochs, you need to train the model for larger number of epochs for better performance.
Related
I am relatively new to the neural network, so I was trying to use it for unsupervised clustering. My data is in dataframe with 5 different columns (features), I wanted to get like 4 classes from this, see the full model below
from sklearn import preprocessing as pp
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import log_loss
from sklearn.metrics import precision_recall_curve, average_precision_score
from sklearn.metrics import roc_curve, auc, roc_auc_score
import keras
from keras import backend as K
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout , Flatten
from keras.layers import BatchNormalization, Input, Lambda
from keras import regularizers
from keras.losses import mse, categorical_crossentropy
model = Sequential()
model.add(Dense(32, activation='relu',input_shape=[5]))
model.add(Flatten())
model.add(Dense(units=32, activation='relu'))
model.add(Dense(units=16, activation='relu'))
model.add(Dense(units=4, activation='relu'))
model.add(Dense(4, activation = "softmax"))
model.compile(optimizer='adam',loss="categorical_crossentropy",metrics=['accuracy'])
when I give the option of generating 4 classes I get the error message:
ValueError: Shapes (None, 5) and (None, 4) are incompatible
I don't know what I am doing wrong. I have tried to use a different loss function, same error.
i get the error when i input my data,
out_class = model.fit(x=pd_pca_std,
y=pd_pca_std,
epochs=num_epochs,
batch_size=batch_size,
shuffle=True,
validation_data=(pd_pca_std, pd_pca_std),
verbose=1)
the values are
batch_size = 33
epochs = 20
num_classes = 4
input_shape = (990000, 5)
output_shape = (990000, 4)
I would suggest using 5 classes or something relative to the 5 classes instead. I'll explain.
So in Neural Networks and machine learning in general, there are certain matrix operations that happen in the background in TensorFlow. So say I create the following:
import numpy as np
x = np.random.random((3, 4))
y = np.random.random((3, 3))
np.dot(x, y) # if I try multiplying 2 incompatible matrices, the program will fail :(
So what's happening here is that the matrices are incompatible for simple matrix arithmetic, because they need to be certain shapes for them to be compatible. So what I recommend doing is either changing the shapes of the matrices/arrays in question, or play with different shapes in the program to see which will succeed...
You could also learn some linear algebra which has the rules for matrix manipulation and arithmetic, but I won't go into that right now. However, what I will do is leave a link for you to check out regarding this subject so you know what to do in the future...
Here it is:
https://www.mathlynx.com/online/LinAlg_Matrices_rules
Hopefully this helps...
Have a nice day :)
TL;DR
Your output units should match the number of classes you are testing on.
This is the skeleton of how I replicated you problem and had it working
from sklearn import preprocessing as pp
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import log_loss
from sklearn.metrics import precision_recall_curve, average_precision_score
from sklearn.metrics import roc_curve, auc, roc_auc_score
import numpy as np
import keras
from keras import backend as K
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout , Flatten
from keras.layers import BatchNormalization, Input, Lambda
from keras import regularizers
from keras.losses import mse, categorical_crossentropy
X = '''input data here as an array''' # I used X = np.zeros((990000, 5))
y = '''output data here as an array'''#I used y = np.ones((990000, 4))
batch_size = 33
num_epochs = 20
num_classes = 4
model = Sequential()
model.add(Dense(32, activation='relu',input_shape=X.shape[1:])) #Input shape = 5
model.add(Flatten())
model.add(Dense(units=32, activation='relu'))
model.add(Dense(units=16, activation='relu'))
model.add(Dense(units=4, activation='relu'))
model.add(Dense(y.shape[1], activation = "softmax")) #Output = y.shape[1] = 4
model.compile(optimizer='adam',loss="categorical_crossentropy",metrics=['accuracy'])
model.summary() #Will show you a summary of the model
model.fit(x=X, y=y,epochs=num_epochs, batch_size=batch_size, shuffle=True,validation_data=(X,y),verbose=1) #You may want to use different variables in your validation.
I've been trying to create a model that recognizes different singing techniques. I have got good results but I want to do different tests with different optimizers, layers, etc. However, I can't get reproducible results. By running twice this model training:
num_epochs = 100
batch_size = 128
history = modelo.fit(X_train_f, Y_train, validation_data=(X_test_f,Y_test), epochs=num_epochs, batch_size=batch_size, verbose=2)
I can get 25% accuracy the first run and then 34% the second. Then if I change the optimizer from "sgd" to "adam", I would get a 99%. If I come back to the previous "sgd" optimizer that got me 34% the second run, I would get 100% or something crazy like that. I don't understand why.
I've tried many things I've read in similar questions. The following lines show how I am trying to make my code to be reproducible, and these are actually the first lines of my whole code:
import numpy as np
import tensorflow as tf
import random as rn
import os
#https://stackoverflow.com/questions/57305909/tensorflow-keras-reproducibility-problem-on-google-colab
os.environ['PYTHONHASHSEED']=str(5)
np.random.seed(5)
rn.seed(12345)
session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
tf.compat.v1.set_random_seed(1234)
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
tf.compat.v1.keras.backend.set_session(sess)
Question is, what am I doing wrong with the code above that is not working (as I mentioned)?
Here's where I create the training sets:
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.convolutional import Conv1D, MaxPooling1D
from keras.layers.core import Dense, Flatten
from keras.layers import BatchNormalization,Activation
from keras.optimizers import SGD, Adam
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=2)
My model:
from tensorflow.keras import layers
from tensorflow.keras import initializers
input_dim = X_train_f.shape[1]
output_dim = Y_train.shape[1]
modelo = Sequential()
modelo.add(Conv1D(filters=6, kernel_initializer=initializers.glorot_uniform(seed=5), kernel_size=5, activation='relu', input_shape=(40, 1))) # 6
modelo.add(MaxPooling1D(pool_size=2))
modelo.add(Conv1D(filters=16, kernel_initializer=initializers.glorot_uniform(seed=5), kernel_size=5, activation='relu')) # 16
modelo.add(MaxPooling1D(pool_size=2))
modelo.add(Flatten())
modelo.add(Dense(120, kernel_initializer=initializers.glorot_uniform(seed=5), activation='relu')) # 120
modelo.add(Dense(84, kernel_initializer=initializers.glorot_uniform(seed=5), activation='relu')) # 84
modelo.add(Dense(nclases, kernel_initializer=initializers.glorot_uniform(seed=5), activation='softmax'))
sgd = SGD(lr=0.1)
#modelo.compile(loss='categorical_crossentropy',
# optimizer='adam',
# metrics=['accuracy'])
modelo.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
modelo.summary()
modelo.input_shape
It is a normal situation. Adam optimizer is much more powerful comparing to SGD. Adam implicitly performs coordinate-wise gradient clipping and can hence, unlike SGD, tackle heavy-tailed noise.
Keras prediction returns the same value everywhere.
I have some xyz data that I want to predict in a regular grid, using keras ML.
I am using something wrong and can't figure it out.
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import Adadelta, Adam
m=1e5
data=np.random.rand(m,3) # let's generate some random data (i do actually have real data that make sense)
dx=0.05
xmin=np.min(data[:,0])
xmax=np.max(data[:,0])
ymin=np.min(data[:,1])
ymax=np.max(data[:,1])
xs=np.arange(xmin,xmax+dx,dx)
ys=np.arange(ymin,ymax+dx,dx)
xg,yg=np.meshgrid(xs,ys)
shape = (len(ys), len(xs))
activation='sigmoid'
hidden_layer_sizes=[128, 64, 32, 16]
keras_model = Sequential()
keras_model.add(Dense(hidden_layer_sizes[0], activation=activation, input_shape=(2, )))
for hl_size in hidden_layer_sizes[1: ]:
keras_model.add(Dense(hl_size, activation=activation))
keras_model.add(Dense(1))
keras_model.compile(loss='mean_squared_error', optimizer=Adam())
keras_model.save_weights('cache.h5')
keras_model.summary()
keras_model.load_weights('cache.h5') # re-initialize Keras model weights
keras_history = keras_model.fit(data[:,:2], data[:,2], batch_size=m, epochs=20000, verbose=1)
X_test = np.vstack((xg.flatten(), yg.flatten())).T
res_keras=keras_model.predict(X_test).reshape(shape)
I am expecting some values "close" to an interpolation function.
Where is the mistake in my code?
change you activation from sigmoid to relu
set
activation='relu'
I used a simple regression problem as below to illustrate my question.
I have a model with various layers. For two specific layers, conv1 and dense2, they have parameters in size of "2x2 = 4" and "4x3 + 1x3 = 15" respectively.
I want to import the weight into the layers before doing the training. To be specific, I want to import w_conv=np.array([[1,2],[3,4]]) into conv1, and I want to import w_dense = np.array([[2,3,1],[6,7,7],[8,9,4],[5,4,8]]) and b_dense = np.array([4,7,9]) into dense2. What should I do to import those values?
Besides, is it also possible to have some codes to check the value of the weight of the layers, before and after importing the values? Many thanks!
from __future__ import absolute_import, division, print_function
from scipy import misc
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Activation
import numpy as np
import matplotlib.pyplot as plt
img1=np.array([[3,5,7,8],[4,7,9,6],[2,9,7,3],[0,2,4,7]])
img2=np.array([[4,6,2,7],[9,2,1,5],[7,5,4,8],[0,8,1,4]])
data1=np.reshape(img1,(4,4,1))
data2=np.reshape(img2,(4,4,1))
train_images=np.stack((data1, data2))
train_labels=np.array([[2,8,9],[1, 0, 7]])
def simple_model():
input = keras.layers.Input((4,4,1))
a=keras.layers.Conv2D(kernel_size=2,filters=1,strides=1, \
padding='same', use_bias=False, name='conv1')(input)
b=output = keras.layers.Flatten()(a)
c = keras.layers.Dense(4, activation=tf.nn.sigmoid)(b)
output = keras.layers.Dense(3, activation=tf.nn.sigmoid)(c)
model = keras.models.Model(input, output)
return model
model = simple_model()
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy','mean_squared_error'])
print(model.summary())
model.fit(train_images, train_labels, epochs=2)
You can use the get_weights() and set_weights() to get and set the weights of a layer respectively.
Iterate through the layers and set the weights:
for layer in model.layers:
print(layer.name)
input_1
conv1
flatten
dense
dense_1
print(model.layers[1].get_weights()[0].shape)
(2, 2, 1, 1) # (kernel_size), ip_channels, op_channels
Setting the weights of conv1 layer by knowing the layer index:
w_conv=np.array([[1,2],[3,4]])
w_conv = np.expand_dims(np.expand_dims(w_conv, -1), -1)
# pass a list of weights, since bias is false, the list has only one element
model.layers[1].set_weights([w_conv])
# get the weights back, first element of the list
assert np.array_equal(w_conv, model.layers[1].get_weights()[0])
Similarly, for the last dense layer:
w_dense = np.array([[2,3,1],[6,7,7],[8,9,4],[5,4,8]])
b_dense = np.array([4,7,9])
model.layers[-1].set_weights([w_dense, b_dense])
model.layers[-1].get_weights()
I am running the test script from the Keras website for Multilayer Perceptron (MLP) for multi-class softmax classification. Running in the jupyter notebook I get the error "name 'keras' is not defined". This may be a simple python syntax problem that I am not keen to, however this code comes straight from keras so I expect it should work as is. I have run other neural nets using keras, so I am pretty sure that I have installed everything (installed keras using anaconda). Can anyone help? I have included both the code and the error at the bottom. Thanks!
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
model.fit(x_train, y_train,
epochs=20,
batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
This is the error message:
NameError Traceback (most recent call last)
<ipython-input-1-6d8174e3cf2a> in <module>()
6 import numpy as np
7 x_train = np.random.random((1000, 20))
----> 8 y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
9 x_test = np.random.random((100, 20))
10 y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
NameError: name 'keras' is not defined
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
From above, you only imported following submodules in keras
keras.models
keras.layers
keras.optimizers
But this does not automatically import the outer module like keras
or other submodules keras.utils
So, you can do either one
import keras
import keras.utils
from keras import utils as np_utils
but from keras import utils as np_utils is the most widely used.
Especially import keras is not a good practice because importing the higher module does not necessarily import its submodules (though it works in Keras)
For example,
import urllib
does not necessarily import urllib.request because if there are so many big submodules, it's inefficient to import all of its submodules every time.
EDIT:
With the introduction of Tensorflow 2, keras submodules such as keras.utils should now be imported as
from tensorflow.keras import utils as np_utils
General way:
from keras.utils import to_categorical
Y_train = to_categorical(y_train, num_classes)
Concrete way:
from keras.utils import to_categorical
print(to_categorical(1, 2))
print(to_categorical(0, 2))
Will output
[0. 1.]
[1. 0.]
Although this is an old question but yet updating the latest approach to access to_categorical function.
This function has now been packed in np_utils.
The correct way to access it is:
from keras.utils.np_utils import to_categorical
This works for me:
import tensorflow as tf
from keras import utils as np_utils
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)