I am experimenting with LSTM encoder-decoder. It is not clear to me who should I reshape the input data.
I used the following code:
import keras
import random
import numpy as np
from random import randint
from numpy import array
from numpy import argmax
from pandas import DataFrame
from pandas import concat
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import TimeDistributed
from keras.layers import RepeatVector
cardinality= 10
n_steps=10
n_steps_y=3
n_features=1
def getRandomInt():
return getOneHotEncoded(random.randint(1,cardinality),cardinality)
def getOneHotEncoded(value, cardinality):
encoded = [0 for _ in range(cardinality+1)]
encoded[value] = 1
return encoded
def generateXY():
X, y = list(), list()
for q in range(100):
x_temp = [getRandomInt() for _ in range(10)]
y_temp = x_temp[-3:]
X.append(x_temp)
y.append(y_temp)
return np.array(X), np.array(y)
def getModel(n_steps=n_steps,n_features=n_features):
model = Sequential()
model.add(LSTM(12, input_shape=(n_steps,n_features)))
model.add(RepeatVector(n_steps_y))
model.add(LSTM(5, return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='categorical_crossentropy',optimizer='adam')
print(model.summary())
return model
X,y = generateXY()
model=getModel()
model.fit(X,y, epochs=10, batch_size=10,verbose=1)
and got error about the shape of the input.
ValueError: Error when checking input: expected lstm_1_input to have
shape (10, 1) but got array with shape (10, 11)
how should I reshape the input appropriately for this code?
I think what you are trying to do is passing a sequence of arrays of one-hot-encoded random numbers. Your sequences are 10 long and your arrays are 11 long.
To represent that, you need to set n_steps = 10 and n_features = 11
By the way: In encoded = [0 for _ in range(cardinality+1)], I don't quite understand the reasoning behind cardinality+1. You don't need to add 1 to represent the numbers from 0 to 9. If you change it to encoded = [0 for _ in range(cardinality)], you can set n_features = 10.
I hope this helped.
Related
I got a ValueError when using TensorFlow to create a model. Based on the error there is a problem that occurs with the kernel regularizer applied on the Conv2D layer and the mean squared error function. I used the L1 regularizer provided by the TensorFlow keras package. I've tried setting different values for the L1 regularization factor and even setting the value to 0, but I get the same error.
Context: Creating a model that predicts phenotype traits given genotypes and phenotypes datasets. The genotype input data has 4276 samples, and the input shape that the model takes is (28220,1). My labels represent the phenotype data. The labels include 4276 samples with 20 as the number of phenotype traits in the dataset. In this model we use differential privacy(DP) and add it to a CNN model which uses the Mean squared error loss function and the DPKerasAdamOptimizer to add DP. I'm just wondering if MSE would be a good choice as a loss function?
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
!pip install tensorflow-privacy
import numpy as np
import tensorflow as tf
from tensorflow_privacy import *
import tensorflow_privacy
from matplotlib import pyplot as plt
import pylab as pl
import numpy as np
import pandas as pd
from tensorflow.keras.models import Model
from tensorflow.keras import datasets, layers, models, losses
from tensorflow.keras import backend as bke
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.regularizers import l1, l2, l1_l2 #meaning of norm
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
batch_size = 32
epochs = 4
microbatches = 8
inChannel = 1
kr = 0#1e-5
num_kernels=8
drop_perc=0.25
dim = 1
l2_norm_clip = 1.5
noise_multiplier = 1.3
learning_rate = 0.25
latent_dim = 0
def print_datashape():
print('genotype data: ', genotype_data.shape)
print('phenotype data: ', single_pheno.shape)
genotype_data = tf.random.uniform([4276, 28220],1,3, dtype=tf.dtypes.int32)
phenotype_data = tf.random.uniform([4276, 20],-4.359688,34,dtype=tf.dtypes.float32)
genotype_data = genotype_data.numpy()
phenotype_data = phenotype_data.numpy()
small_geno = genotype_data
single_pheno = phenotype_data[:, 1]
print_datashape()
df = small_geno
min_max_scaler = preprocessing.MinMaxScaler()
df = min_max_scaler.fit_transform(df)
scaled_pheno = min_max_scaler.fit_transform(single_pheno.reshape(-1,1)).reshape(-1)
feature_size= df.shape[1]
df = df.reshape(-1, feature_size, 1, 1)
print("df: ", df.shape)
print("scaled: ", scaled_pheno.shape)
# split train to train and valid
train_data,test_data,train_Y,test_Y = train_test_split(df, scaled_pheno, test_size=0.2, random_state=13)
train_X,valid_X,train_Y,valid_Y = train_test_split(train_data, train_Y, test_size=0.2, random_state=13)
def print_shapes():
print('train_X: {}'.format(train_X.shape))
print('train_Y: {}'.format(train_Y.shape))
print('valid_X: {}'.format(valid_X.shape))
print('valid_Y: {}'.format(valid_Y.shape))
input_shape= (feature_size, dim, inChannel)
predictor = tf.keras.Sequential()
predictor.add(layers.Conv2D(num_kernels, (5,1), padding='same', strides=(12, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L1(kr),input_shape= input_shape))
predictor.add(layers.AveragePooling2D(pool_size=(2,1)))
predictor.add(layers.Dropout(drop_perc))
predictor.add(layers.Flatten())
predictor.add(layers.Dense(int(feature_size / 4), activation='relu'))
predictor.add(layers.Dropout(drop_perc))
predictor.add(layers.Dense(int(feature_size / 10), activation='relu'))
predictor.add(layers.Dropout(drop_perc))
predictor.add(layers.Dense(1))
mse = tf.keras.losses.MeanSquaredError(reduction=tf.keras.losses.Reduction.NONE)
optimizer = DPKerasAdamOptimizer(learning_rate=learning_rate, l2_norm_clip=l2_norm_clip, noise_multiplier=noise_multiplier, num_microbatches=microbatches)
# compile
predictor.compile(loss=mse, optimizer=optimizer, metrics=['mse'])
#summary
predictor.summary()
print_shapes()
predictor.fit(train_X, train_Y,batch_size=batch_size,epochs=epochs,verbose=1, validation_data=(valid_X, valid_Y))
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 0 with other shapes. for '{{node AddN}} = AddN[N=2, T=DT_FLOAT](mean_squared_error/weighted_loss/Mul, conv2d_2/kernel/Regularizer/mul)' with input shapes: [?], [].
Concatenation problem for sequential LSTM network
I've tried to use the Concatenate functions to combine two input arrays axis,
but I get the following error, anyone know how to concatenate that arrays? maybe in more steps of concatenations?
cc1 = Concatenate([inp1, inp2],axis=2)
TypeError: init () got multiple values for argument 'axis'
Output Logs
b'Hello, TensorFlow!'
42
Using TensorFlow backend.
features_set
(1200, 60)
features_set3
(1, 1200, 60)
C:\ProgramData\Anaconda3\lib\sitepackages\sklearn\utils\validation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.warnings.warn(msg, DataConversionWarning)
features_set2
(1200, 60)
features_set4
(1, 1200, 60)
shapeINP1
(?, 1, 1200, 60)
shapeINP2
(?, 1, 1200, 60)
Traceback (most recent call last):
File "C:\Users\User\Documents\priv\progpy\prog10-t12.py", line 81, in
cc1 = Concatenate([inp1, inp2],axis=2)
TypeError: __init__() got multiple values for argument 'axis'
Scope
The program here below takes prices of the stocks, then takes the future prices as labels to train the keras net to predict the label prices.
I was tring to add to the prices input a new input, the new input to add is the volume input, both will be concatenated to predict the future stock label prices.
Program
import re
import urllib
import json
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
import numpy
import numpy as np
from keras.models import Model
from keras.layers import Add
from keras.layers import Concatenate
from keras.layers import Dense, concatenate, Input
from keras.layers import LSTM
from keras.utils import np_utils
import keras.layers
apple_training_complete = pd.read_csv(r'\Apps\aapltr.csv')
apple_training_processed = apple_training_complete.iloc[:, 5:6].values
scaler = MinMaxScaler(feature_range = (0, 1))
apple_training_scaled = scaler.fit_transform(apple_training_processed)
features_set = []
labels = []
labels2 = []
for i in range(60, 1260):
features_set.append(apple_training_scaled[i-60:i, 0])
labels.append(apple_training_scaled[i, 0])
features_set3 = []
labels2 = []
features_set, labels = np.array(features_set), np.array(labels)
features_set = np.reshape(features_set, (features_set.shape[0], features_set.shape[1]))
print("features_set")
print(features_set.shape)
features_set3.append(features_set)
features_set3 = np.array(features_set3)
features_set3 = np.reshape(features_set3, (features_set3.shape[0], features_set3.shape[1], features_set3.shape[2]))
print("features_set3")
print(features_set3.shape)
apple_training_complete2 = pd.read_csv(r'\Apps\aapltr.csv')
apple_training_processed2 = apple_training_complete2.iloc[:, 6:7].values
apple_training_scaled2 = scaler.fit_transform(apple_training_processed2)
features_set2 = []
for i in range(60, 1260):
features_set2.append(apple_training_scaled2[i-60:i,0])
labels2.append(apple_training_scaled[i, 0])
features_set2, labels2 = np.array(features_set2), np.array(labels2)
features_set2 = np.reshape(features_set2, (features_set2.shape[0], features_set2.shape[1]))
print("features_set2")
print(features_set2.shape)
features_set4 = []
features_set4.append(features_set2)
features_set4 = np.array(features_set4)
features_set4 = np.reshape(features_set4, (features_set4.shape[0], features_set4.shape[1], features_set4.shape[2]))
print("features_set4")
print(features_set4.shape)
inp1 = Input(features_set3.shape)
inp2 = Input(features_set4.shape)
print(" shapeINP1 ")
print(inp1.shape)
print(" shapeINP2 ")
print(inp2.shape)
cc1 = Concatenate([inp1, inp2],axis=2)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()
Actual Result
cc1 = Concatenate([inp1, inp2],axis=2)
TypeError: init() got multiple values for argument 'axis'
Expected Result is to use the concatenated inputs to train a LSTM sequential
inp1 = Input(features_set3.shape)
inp2 = Input(features_set4.shape)
cc1 = Concatenate([inp1, inp2],axis=0)
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(inp1, inp2, 1)))
I've alread tried this second version but without results
input_merged = Concatenate([inp1, inp2])(axis=1)
lstm = LSTM(40)(input_merged)
this second version of the code give the this error
prog10-t12.py", line 81,
input_merged = Concatenate([inp1, inp2])(axis=1)
TypeError: call () missing 1 required positional argument: 'inputs'**
Concatenate is not a function, its a layer, so it should be used like:
cc1 = Concatenate(axis=2)([inp1, inp2])
I am finding that my model has a tensor that is of shape (?,1,60). I want to know how I can reduce this to (?,60)? Not sure whether reshape or Flatten can be done with respect to a dimension. Any help?
Both layers will work, but in this case I prefer using keras.layers.Flatten. Here is an example:
from keras.layers import Input, Flatten
from keras.models import Model
import numpy as np
a = Input(shape=(1, 60))
b = Flatten()(a)
model = Model(inputs=a, outputs=b)
model.compile('sgd', 'mse')
pred = model.predict(x=np.ones(shape=(2, 1, 60)))
print(pred.shape)
I am giving variable size images (all 278 images of different size 139 of each category) input to my cnn model. As a fact that cnn required fixed size images, so from here i got solution for this is to make input_shape=(None,Nonen,1) (for tensorflow backend and gray scale). but this solution doesnot work with flatten layer, so from their only i got solution of using GlobleMaxpooling or Globalaveragepooling. So from uses these facrts i am making a cnn model in keras to train my network with following code:
import os,cv2
import numpy as np
from sklearn.utils import shuffle
from keras import backend as K
from keras.utils import np_utils
from keras.models import Sequential
from keras.optimizers import SGD,RMSprop,adam
from keras.layers import Conv2D, MaxPooling2D,BatchNormalization,GlobalAveragePooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import regularizers
from keras import initializers
from skimage.io import imread_collection
from keras.preprocessing import image
from keras import Input
import keras
from keras import backend as K
#%%
PATH = os.getcwd()
# Define data path
data_path = PATH+'/current_exp'
data_dir_list = os.listdir(data_path)
img_rows=None
img_cols=None
num_channel=1
# Define the number of classes
num_classes = 2
img_data_list=[]
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img,0)
img_data_list.append(input_img)
img_data = np.array(img_data_list)
if num_channel==1:
if K.image_dim_ordering()=='th':
img_data= np.expand_dims(img_data, axis=1)
print (img_data.shape)
else:
img_data= np.expand_dims(img_data, axis=4)
print (img_data.shape)
else:
if K.image_dim_ordering()=='th':
img_data=np.rollaxis(img_data,3,1)
print (img_data.shape)
#%%
num_classes = 2
#Total 278 sample, 139 for 0 category and 139 for category 1
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')
labels[0:138]=0
labels[138:]=1
x,y = shuffle(img_data,labels, random_state=2)
y = keras.utils.to_categorical(y, 2)
model = Sequential()
model.add(Conv2D(32,(2,2),input_shape=(None,None,1),activation='tanh',kernel_initializer=initializers.glorot_uniform(seed=100)))
model.add(Conv2D(32, (2,2),activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (2,2),activation='tanh'))
model.add(Conv2D(64, (2,2),activation='tanh'))
model.add(MaxPooling2D())
model.add(Dropout(0.25))
#model.add(Flatten())
model.add(GlobalAveragePooling2D())
model.add(Dense(256,activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(2,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
model.fit(x, y,batch_size=1,epochs=5,verbose=1)
but i am getting following error:
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (278, 1)
how to solve it.
In the docs for Conv2D it says that the input tensor has to be in this format:
(samples, channels, rows, cols)
I believe you can't have a variable input size unless your network is fully convolutional.
Maybe what you want to do is to keep it to a fixed input size, and just resize the image to that size before feeding it into your network?
Your array with input data cannot have variable dimensions (this is a numpy limitation).
So the array, instead of being a regular array of numbers with 4 dimensions is being created as an array of arrays.
You should fit each image individually because of this limitation.
for epoch in range(epochs):
for img,class in zip(x,y):
#expand the first dimension of the image to have a batch size
img = img.reshape((1,) + img.shape)) #print and check there are 4 dimensions, like (1, width, height, 1).
class = class.reshape((1,) + class.shape)) #print and check there are two dimensions, like (1, classes).
model.train_on_batch(img,class,....)
Im trying to understand how to use LSTM to classify a certain dataset that i have.
I researched and found this example of keras and imdb :
https://github.com/fchollet/keras/blob/master/examples/imdb_lstm.py
However, im confused about how the data set must be processed to input.
I know keras has pre-processing text methods, but im not sure which to use.
The x contain n lines with texts and the y classify the text by happiness/sadness. Basically, 1.0 means 100% happy and 0.0 means totally sad. the numbers may vary, for example 0.25~~ and so on.
So my question is, How i input x and y properly? Do i have to use bag of words?
Any tip is appreciated!
I coded this below but i keep getting the same error:
#('Bad input argument to theano function with name ... at index 1(0-based)',
'could not convert string to float: negative')
import keras.preprocessing.text
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM
print('Loading data...')
import pandas
thedata = pandas.read_csv("dataset/text.csv", sep=', ', delimiter=',', header='infer', names=None)
x = thedata['text']
y = thedata['sentiment']
x = x.iloc[:].values
y = y.iloc[:].values
###################################
tk = keras.preprocessing.text.Tokenizer(nb_words=2000, filters=keras.preprocessing.text.base_filter(), lower=True, split=" ")
tk.fit_on_texts(x)
x = tk.texts_to_sequences(x)
###################################
max_len = 80
print "max_len ", max_len
print('Pad sequences (samples x time)')
x = sequence.pad_sequences(x, maxlen=max_len)
#########################
max_features = 20000
model = Sequential()
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128, input_length=max_len, 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='rmsprop')
model.fit(x, y=y, batch_size=200, nb_epoch=1, verbose=1, validation_split=0.2, show_accuracy=True, shuffle=True)
# at index 1(0-based)', 'could not convert string to float: negative')
Review how you are using your CSV parser to read the text in. Ensure that the fields are in the format Text, Sentiment if you want to to make use of the parser as you've written it in your code.