LSTM multiple features, multiple classes, multiple outputs
I'm trying to use a LSTM classifier to generate music based on some midi's that I have.
The LSTM uses two features, the notes' pitch and the notes' duration.
For illustration, let's think we have:
Pitches: ["A", "B", "C"]
Durations: ["0.5", "1", "1.5"]
As you can imagine, a generated note has to have both pitch and duration.
I tried to do it with a MultiLabelBinarizer.
from sklearn.preprocessing import MultiLabelBinarizer
labels = [[x,y] for x in all_pitches for y in all_durations]
mlb = MultiLabelBinarizer()
mlb_value = mlb.fit_transform(labels)
This divides the classes as intended, but the problem I'm having comes at the time of predictions.
prediction = model.predict_proba(prediction_input)
indexes = np.argsort(prediction, axis=None)[::1]
index1 = indexes[0]
index2 = indexes[1]
result1 = mlb.classes_[index1]
result2 = mlb.classes_[index2]
I need the notes to have both pitch and duration, so this approach seems to not work for me (I only get the same two pitches all over).
Another thing I thought was using a MultiOutputClassifier
, but I seem unable to understand the differences of them, or how to actually use this MultiOutputClassifier
correctly.
Thanks for the patience, and sorry for the probably stupid question.
1 answer

You can feed your LSTM output into many different layers (or neural functions, in general), which lead to different outputs, and then train your model on each of these outputs concurrently:
from keras.models import Model from keras.layers import Input, Dense, LSTM # function definitions lstm_function = LSTM(..args) pitch_function = Dense(num_pitches, activation='softmax') duration_function = Dense(num_durations, activation='softmax') input_features = Input(input_dimensionality) # function applications lstm_output = lstm_function(input_features) pitches = pitch_function(lstm_output) durations = duration_function(lstm_output) # model model = Model(inputs=[input_features], outputs=[pitches, durations]) model.compile(loss=['categorical_crossentropy', 'mse'], optimizer='RMSProp')
This may be generalized to arbitrary information flows, with as many layers/outputs as you need. Remember that for each output you need to define a corresponding loss (or
None
).
See also questions close to this topic

Showing all the images with matplotlib
I'm using numpy and matplotlib to read all the images in the folder for image processing techniques. Although, I have done the part of reading image dataset from folders and process it with numpy array. But the problem, I'm facing is of showing all the images with matplotlib.imshow function. Everytime I want to show all the images with imshow function, unfortunately it just give me first image nothing else. My code is below:
import os import numpy as np import matplotlib.pyplot as mpplot import matplotlib.image as mpimg images = [] path = "../path/to/folder" for root, _, files in os.walk(path): current_directory_path = os.path.abspath(root) for f in files: name, ext = os.path.splitext(f) if ext == ".jpg": current_image_path = os.path.join(current_directory_path,f) current_image = mpimg.imread(current_image_path) images.append(current_image) for img in images: print len(img.shape) i = 0 for i in range(len(img.shape)): mpplot.imshow(img) mpplot.show()
I will be thankful if somebody can help me in this.
P.S. I'm pretty new with python, numpy and also at stackoverflow. So, please don't mind if the question is unclear or not direct.
Thanks,

Tkinter, changing a button and its command
We have a button that calls function A. We want function A to change the image of that button and to change its command to call function B instead of A. How to do that? We can't have two buttons at the same time.
Thank you in advance.

SYN flooding in Python
Is there any way to perform SYN flooding on a target ip address? I have tried many programs but none of them have seem to flood the targeted ip addresses. Kindly help.

python while loop to ask an input from user twice
Write a program that uses while loops to perform the following steps: (3) a. Prompt the user to input two integers: firstNum and secondNum (firstNum must be less than secondNum). b. Output all odd numbers between firstNum and secondNum. c. Output the sum of all even numbers between firstNum and secondNum. d. Output the numbers and their squares between 1 and 10. e. Output the sum of the square of the odd numbers between firstNum and secondNum.

Output data on same page after form submit
So I created a small flask program which would take a file , do some processing and returns a stream of data using
yield
.I am using html form for file upload and submit. The form sends file to a python script and returns the output. The issue is that the output is presented onto a different page because of the form action attribute whereas I need the output on the same page. Probably inside a
div
tag.index.html
<script> if (!!window.EventSource) { var source = new EventSource('/upload'); source.onmessage = function(e) { console.log(e) var byte = e.data; var res = byte.split(/\s/); console.log(res[0]) $("#morse").text(res[0].slice(1,)); } } </script> <form action="/upload" method=post enctype=multipart/formdata > <p><input type="file" name="file" > <input type="submit" value="Upload" id="search_form_input"> </form> <div id="morse" class="info">nothing received yet</div> // this is where is need my data
Python code
@app.route('/') def index(): return render_template('index.html') @app.route("/upload", methods=['GET', 'POST']) def streambyte(): if request.method == 'POST': f = request.files['file'] list_of_items = unAssign(f) # some file processing def events(): for i in list_of_items: yield "data: %s\n\n" % (i) time.sleep(1) # an artificial delay return Response(events(), content_type='text/eventstream')
This streams the data on
http://localhost:5000/upload
whereas I need it onhttp://localhost:5000
.I tried using redirect with Response but it failed saying
TypeError: 'generator' object is not callable

create set of randomized column names in pandas dataframe
I am trying to create a set of columns (within panda dataframe) where the column names are randomized. This is because I want to generate filter data from a larger dataset in a randomized fashion.
How can I generate an N (= 4) * 3 set of column names as per below?
car_speed state_8 state_17 state_19 state_16 wd_8 wd_17 wd_19 wd_16 wu_8 wu_17 wu_19 wu_16
My potential code below, but doesn't really work. I need the blocks'state_' first, then 'wd_', and then 'wd_'. My code below generates 'state_', 'wd_', 'wu_' individually in consecutive order. I have problems further on, when it is in that order, of filling in the data from the larger dataset
def iteration1(data, classes = 50, sigNum = 4): dataNN = pd.DataFrame(index = [0]) dataNN['car_speed'] = np.zeros(1) while len(dataNN.columns) < sigNum + 1: state = np.int(np.random.uniform(0, 50)) dataNN['state_'+str(state)] = np.zeros(1) # this is the state value setup dataNN['wd_' + str(state)] = np.zeros(1) # this is the weight direction dataNN['wu_' + str(state)] = np.zeros(1) # this is the weight magnitude count = 0 # initialize count row as zero while count < classes : dataNN.loc[count] = np.zeros(len(dataNN.columns)) for state in dataNN.columns[1:10]: dataNN[state].loc[count] = data[state].loc[count] count = count + 1 if count > classes : break return dataNN

Python predict_proba
I have a question on a classification problem in machine learning using the log_loss function in scikit learn.
from sklearn.ensemble import RandomForestClassifier classifier = RandomForestClassifier() classifier.fit(Xtrain, ytrain) soft = classifier.predict_proba(Xtest)[:,1] log_loss = log_loss(ytest, soft)
I would to compute the log loss but an error appears :
'numpy.float64' object is not callable
I think that this problem may come from the fact that there is some 0 in the vector soft. But I do know to solve this problem ?
s = 0 for x in soft : if x == 0 : s+=1 print(s) >> 17729
Thanks in advance

Scikitlearn regression dealing with both linear and nonlinear multidimensional data
I have a situation where my data contains some nonlinear functions which can be predicted well with a Gaussian kernel, and some linear (or close to) functions which cause rapid divergence when used with the Gaussian kernel. Is there a method for dealing with such cases?
I've included a minimal example which I hope demonstrates this issue, where if data[:, 1] = np.sin(t) is used the ML converges correctly while if data[:, 1] = np.linspace(1, 10, num=data_size) is used the system diverges.
import numpy as np from sklearn.model_selection import GridSearchCV from sklearn.gaussian_process import GaussianProcessRegressor from matplotlib import pyplot as plt from sklearn.gaussian_process.kernels import (RBF, Matern, RationalQuadratic, ExpSineSquared, DotProduct, ConstantKernel, WhiteKernel) data_size = int(1e3) training_size = int(1e2) t = np.sin(np.linspace(0, 50, num=data_size)) data = np.zeros((data_size, 2)) output = np.zeros((data_size, 2)) data[:, 0] = np.cos(t) #data[:, 1] = np.sin(t) data[:, 1] = np.linspace(1, 10, num=data_size) output[:, 0] = np.sin(data[:, 0]) gpr = GaussianProcessRegressor(kernel=1.0 * RBF(1.0), normalize_y=False, optimizer='fmin_l_bfgs_b', alpha=1e6) gpr.fit(data[:training_size], output[:training_size]) gpr_results = gpr.predict(data) plt.plot(gpr_results[:, 0], 'r') plt.plot(output[:, 0], 'b') plt.show()

contour plot for regression predict with fixed input variables
I want to create a contour plot for a prediction with multiple features. The remaining values should be fixed to plot the 2 interesting values. Unfortunately I resulting matrix has the same value on all positions instead of the expected.
I think something with my matrixes is wrong, but I don't find the error.
[...] f_learn = [x_1,x_2,x_3,x_4] r_lear = [r_1] clf = svm.MLPRegressor(...) clf.fit(f_learn,r_learn) [...] x_1 = np.linspace(1, 100, 100) x_2 = np.linspace(1, 100, 100) X_1, X_2 = np.meshgrid(x_1, x_2) x_3 = np.full( (100,100), 5).ravel() x_4 = np.full( (100,100), 15).ravel() predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3,x_4]) prediction = clf.predict(predict_matrix.T) prediction_plot = prediction.reshape(X_1.shape) plt.figure() cp = plt.contourf(X_1, X_2, prediction_plot, 10) plt.colorbar(cp) plt.show()
If I test the matrix line by line by hand I get the right results. However, it doesn't work if I put them together this way.
Edit: made a error copying the code
Example with Data. All answer are 7.5 and not diffrent ;(
import matplotlib.pyplot as plt import numpy as np from sklearn import linear_model f_learn = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]) r_learn = np.array([6,7,8,9]) reg = linear_model.LinearRegression() reg.fit (f_learn, r_learn) x_1 = np.linspace(0, 20, 10) x_2 = np.linspace(0, 20, 10) X_1, X_2 = np.meshgrid(x_1, x_2) x_3 = np.full( (10,10), 5).ravel() x_4 = np.full( (10,10), 2).ravel() predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3, x_4]) prediction = reg.predict(predict_matrix.T) prediction_plot = prediction.reshape(X_1.shape) plt.figure() cp = plt.contourf(X_1, X_2, prediction_plot, 10) plt.colorbar(cp) plt.show()

Organising subfolders for keras flow_from_directory?
I'm trying to classify images using Keras .flow_from_directory(directory). I did split the data in the training, validation and test sets.
My main question is: how am I supposed to organise my test folder? I am aware that both validation and train folders need to contain n subdirectories, where n is the amount of classes.
Should my test folder look the same so I could run evalute_generator on it? Or should I just put all the pictures in there and do some custom evaluation?
What is a better practice?

Understanding CNN in NLP
I am very beginner at DeepLearning&CNN. I am going to build a model in which sentence representations will be moved to a CNN. The last hidden layer of CNN is going to be forwarded to an LSTM. At the end of this process, a vector will be produced which will be used in an attention mechanism. My question here is, i have a set of sentences to be used in CNN first. The model that i use is as given below:
inputs = Input(shape=(sequence_length,), dtype='int32') embedding = Embedding(input_dim=vocabulary_size, output_dim=embedding_dim, input_length=sequence_length)(inputs) reshape = Reshape((sequence_length,embedding_dim,1))(embedding) conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], embedding_dim), padding='valid', kernel_initializer='normal', activation='relu')(reshape) conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], embedding_dim), padding='valid', kernel_initializer='normal', activation='relu')(reshape) conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], embedding_dim), padding='valid', kernel_initializer='normal', activation='relu')(reshape) maxpool_0 = MaxPool2D(pool_size=(sequence_length  filter_sizes[0] + 1, 1), strides=(1,1), padding='valid')(conv_0) maxpool_1 = MaxPool2D(pool_size=(sequence_length  filter_sizes[1] + 1, 1), strides=(1,1), padding='valid')(conv_1) maxpool_2 = MaxPool2D(pool_size=(sequence_length  filter_sizes[2] + 1, 1), strides=(1,1), padding='valid')(conv_2) concatenated_tensor = Concatenate(axis=1)([maxpool_0, maxpool_1, maxpool_2]) flatten = Flatten()(concatenated_tensor) dropout = Dropout(drop)(flatten) output = Dense(units=2, activation='softmax')(dropout) # this creates a model that includes model = Model(inputs=inputs, outputs=output) checkpoint = ModelCheckpoint('weights.{epoch:03d}{val_acc:.4f}.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='auto') adam = Adam(lr=1e4, beta_1=0.9, beta_2=0.999, epsilon=1e08, decay=0.0) model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy']) print("Traning Model...") model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[checkpoint], validation_data=(X_test, y_test)) # starts training
As i mentioned before, i have only a set of sentences X_train. There is no target values in my case. How should i handle this? Any help would be greatly appreciated. Thanks in advance.
EDIT1: The system that i am trying to build is a question answering system. This CNN&LSTM procedure will be applied to sentences in the input part.
EDIT2: Actually i am trying to change the input part which is done by GRU to CNN&LSTM. The system is using birectional GRU as below. It is only calculating the vector and moves to the next step. I wonder, instead of using bidirectional GRU, if i change it to CNN&LSTM is possible in this case?
def get_input_representation(self): """Get fact (sentence) vectors via embedding, positional encoding and bidirectional GRU""" # get word vectors from embedding inputs = tf.nn.embedding_lookup(self.embeddings, self.input_placeholder) # use encoding to get sentence representation inputs = tf.reduce_sum(inputs * self.encoding, 2) forward_gru_cell = tf.contrib.rnn.GRUCell(self.config.hidden_size) backward_gru_cell = tf.contrib.rnn.GRUCell(self.config.hidden_size) outputs, _ = tf.nn.bidirectional_dynamic_rnn( forward_gru_cell, backward_gru_cell, inputs, dtype=np.float32, sequence_length=self.input_len_placeholder ) # sum forward and backward output vectors fact_vecs = tf.reduce_sum(tf.stack(outputs), axis=0) # apply dropout fact_vecs = tf.nn.dropout(fact_vecs, self.dropout_placeholder) return fact_vecs

How do we compare the performance of different ConvNets?
I am currently training a net to play a game with a CNN having the following architecture:
model = Sequential() model.add(Conv2D(100, kernel_size=(2, 2), strides=(2, 2), activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(250, activation='relu')) model.add(Dense(classifications, activation='softmax')) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
Now I wish to introduce some complexity in the architecture and make the net deep. How can I tabulate the performance of the CNNs of different complexities and ultimately conclude by giving the best choice for the particular task?
Am i going in the wrong direction? How to decide the depth of a CNN and how does it affect the performance on the same dataset?
Thanks in advance (I am new to this site, kindly excuse the immaturity of this post)

Use both losses on a subnetwork of combined networks
I am trying to stack two networks together. I want to calculate loss of each network separately. For example in the image below; loss of LSTM1 should be (Loss1 + Loss2) and loss of system should be just (Loss2)
I implemented a network like below with the idea above but have no idea how to compile and run it.
def build_lstm1(): x = Input(shape=(self.timesteps, self.input_dim,), name = 'input') h = LSTM(1024, return_sequences=True))(x) scores = TimeDistributed(Dense(self.input_dim, activation='sigmoid', name='dense'))(h) LSTM1 = Model(x, scores) return LSTM1 def build_lstm2(): x = Input(shape=(self.timesteps, self.input_dim,), name = 'input') h = LSTM(1024, return_sequences=True))(x) labels = TimeDistributed(Dense(self.input_dim, activation='sigmoid', name='dense'))(h) LSTM2 = Model(x, labels) return LSTM2 lstm1 = build_lstm1() lstm2 = build_lstm2() combined = Model(inputs = lstm1.input , outputs = [lstm1.output, lstm2(lstm1.output).output)])

create a masking layer in deep learning generative model
I have created a generative model, which trains on padded sequences with a length of 129. I have tried to add a masking layer, but I get the following error message:
input_length" is 129, but received input has shape (None, 129, 7905).
vocab_size = 7905
seq_length_sent = 129
Here is the model:
model = Sequential() model.add(Masking(mask_value=0, input_shape=(seq_length_sent, vocab_size))) model.add(Embedding(vocab_size, 50, input_length=seq_length_sent)) model.add(LSTM(100, return_sequences=True)) model.add(LSTM(100)) model.add(Dense(100, activation='relu')) model.add(Dense(vocab_size, activation='softmax')) print(model.summary())