Concatenating vectors for CNN in Keras/tensorflow
I am trying to concatenate the flattened output of a CNN, and a vector of scalar values. I am trying to influence the network by both the image and the vector. So the output of the flattened CNN size is (1,1024)
, and the vector I want to concatenate is (1,5)
.
Of course keras wants them to be similar sizes. So what is the best practice in this situation? Expand the vector to 1024
with zeroes? I am trying to make the vector have an impact, what other options can I do? I am trying to do something similar to here (https://arxiv.org/abs/1603.02199).
1 answer

Why not concatenating them over the last dimension, to obtain a tensor of shape
(1, 1029)
?from keras.models import Model from keras.layers import Input, Concatenate img = Input(shape=(1,1024)) vec = Input(shape=(1,5)) res = Concatenate(axis=1)([img, vec]) model = Model(inputs=[img, vec], outputs=res) model.summary() # _______________________________________________________________________________ # Layer (type) Output Shape Param # Connected to # =============================================================================== # input_1 (InputLayer) (None, 1, 1024) 0 # _______________________________________________________________________________ # input_2 (InputLayer) (None, 1, 5) 0 # _______________________________________________________________________________ # concatenate_1 (Concatenate) (None, 1, 1029) 0 input_1[0][0] # input_2[0][0] # =============================================================================== # Total params: 0 # Trainable params: 0 # Nontrainable params: 0 # _______________________________________________________________________________
See also questions close to this topic

Fit data into machine learning keras model when data is too huge
In machine learning tutorials using keras, the code to train the machine learning model is this typical oneliner.
model.fit(X_train, Y_train, nb_epoch=5, batch_size = 128, verbose=1, validation_split=0.1)
This seems easy when the training data
X_train
andY_train
is small.X_train
andY_train
are numpy ndarrays. In practical situations, the training data can go into gigabytes which may be too large to be even fitted into the RAM of the computer.How do you send data into
model.fit()
when the training data is too huge? 
Error in classifying SVM texts
I'm trying to apply a text sorting algorithm and unfortunately I have an error
import sklearn import numpy as np from sklearn import svm from sklearn.pipeline import Pipeline from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import CountVectorizer from sklearn.feature_extraction.text import TfidfTransformer from sklearn.metrics import precision_recall_fscore_support as score from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.multiclass import OneVsRestClassifier from sklearn.metrics import accuracy_score from sklearn import metrics from sklearn.metrics import confusion_matrix from sklearn.metrics import precision_recall_fscore_support import pandas as pd import pandas dataset = pd.read_csv('train.csv', encoding = 'utf8') data = dataset['data'] labels = dataset['label'] X_train, X_test, y_train, y_test = train_test_split (data.data, labels.target, test_size = 0.2, random_state = 0) vecteur = CountVectorizer() X_train_counts = vecteur.fit_transform(X_train) tfidf = TfidfTransformer() X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts) clf = MultinomialNB().fit(X_train_tfidf, y_train) #SVM clf = svm.SVC(kernel = 'linear', C = 10).fit(X_train, y_train) print(clf.score(X_test, y_test))
I have the following error:
Traceback (most recent call last):
File "bayes_classif.py", line 22, in
dataset = pd.read_csv('train.csv', encoding = 'utf8')
File "/usr/local/lib/python3.6/distpackages/pandas/io/parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.6/distpackages/pandas/io/parsers.py", line 446, in _read
data = parser.read(nrows)
File "/usr/local/lib/python3.6/distpackages/pandas/io/parsers.py", line 1036, in read
ret = self._engine.read(nrows)
File "/usr/local/lib/python3.6/distpackages/pandas/io/parsers.py", line 1848, in read
data = self._reader.read(nrows)
File "pandas/_libs/parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read
File "pandas/_libs/parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas/_libs/parsers.pyx", line 945, in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx", line 932, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas/_libs/parsers.pyx", line 2112, in pandas._libs.parsers.raise_parser_error pandas.errors.ParserError: Error tokenizing data. C error: Expected 2 fields in line 72, saw 3

TensorFlow : polynomial regression
I am trying to get a nonlinear regression from a CSV data which is available at this link: CSV Data
I want to use polynomial regression. the problem is that the result I am getting from TensorFlow is "None". I cannot find the problem. I think there is something wrong with the model or the cost function. can anybody help? any help would be appreciated.
# importing modules import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import csv import time # defining the method for gathering data # date_idx is the column number of date in the .CSV file def read(filename, date_idx, date_parse, year, bucket=7): # the amount of days in the year : 365 days days_in_year = 365 # defining a dictionary for the frequency freq = {} # here we are calculating hao many buckets each frequency have? # buckets = (which is 7, and by that we mean each frequency is 7 days) # we are initializing each frequency with zero for period in range(0, int(days_in_year / bucket)): freq[period] = 0 # this opens the file in binary mode('rb' : 'r' for read, 'b' is for binary mode) with open(filename, 'r') as csvfile: csvreader = csv.reader(csvfile) next(csvreader) # this escapes the first row since it consists of headers only for row in csvreader: if row[date_idx] == '': # each row consists of many columns but if the date is continue # is unavailable there is no need to check the data t = time.strptime(row[date_idx], date_parse) # converting to the input format if t.tm_year == year and t.tm_yday < (days_in_year1): # we want the data in specific year freq[int(t.tm_yday / bucket)] += 1 # finding the frequency return freq # here i call the method to gather data for me freq = read(r'C:\My Files\Programming\Python\TensorFlow\CallCenter\311_Call_Center_Tracking_Data__Archived_.csv', 0, '%m/%d/%Y', 2014) # here we convert our dictionary into 2 arrays or lists in python x_temp =[] y_temp =[] for key, value in freq.items(): x_temp.append(key) y_temp.append(value) x_data = np.asarray(x_temp) y_data = np.asarray(y_temp) # visualizing the data plt.scatter(x_data,y_data) plt.show() # splitting data with ratio into 2 group : training and test def split_dataset(x_dataset, y_dataset, ratio): arr = np.arange(x_dataset.size) np.random.shuffle(arr) num_train = int(ratio*x_dataset.size) x_train = x_dataset[arr[0:num_train]] y_train = y_dataset[arr[0:num_train]] x_test = x_dataset[arr[num_train:x_dataset.size]] y_test = y_dataset[arr[num_train:y_dataset.size]] return x_train,y_train,x_test,y_test x_train, y_train, x_test, y_test = split_dataset(x_data,y_data, ratio=0.7) # here we create some place holder for input and output of the session X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) # defining global variables learning_rate = 0.01 training_epochs = 100 num_coeffs = 5 # adding regularization (for later use) #reg_lambda = 0. # defining the coefficients of the polynomial w = tf.Variable([0.]*num_coeffs, name='parameter') # defining the model def model(X,w): terms = [] for i in range(num_coeffs): term = tf.multiply(w[i], tf.pow(X, i)) terms.append(term) return tf.add_n(terms) y_model = model(X,w) # defining the cost function cost = tf.reduce_sum(tf.pow(Yy_model,2)) # defining training method train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # initilizing all variables init = tf.global_variables_initializer() #runing the model with tf.Session() as sess: sess.run(init) for epoch in range(training_epochs): training_cost = sess.run(train_op, feed_dict={X:x_train, Y:y_train}) print(training_cost) final_cost = sess.run(cost,feed_dict={X: x_test, Y:y_test}) print('Final cost = {}'.format(training_cost))

How to check number of times a Weight Tensor has been updated in TensorFlow?
I have a simple question. I have created my own weights using
tf.get_variable
. For debugging purposes I need to check how many times the weights have been updated, i.e. how many times the optimizer have actually updated the weight?How can this be done? If you require my code I can provide it.

Text topic classification in tensorflow
I want to create a
CNN
intensorflow
that does the following: Classify a recipe headline and find out the topic. For instancesuper yummy cheesy cake
should result incheese cake
and so on.I thought for going with
tensorflow
, but need some help in getting things started.My strategy is like that:
 Normalize the headlines so
cheesy
becomescheese
andcheesecake
becomescheese cake
for instance and so on. Having a dataset like:
super yummy cheesecake  cheese cake
summer strawberry cake  strawberry cake
Train the model to learn what matters for the topic and what is just additional information.
The way, the dataset is modeled, I have no static lables, as I understand. This makes things complicated, right?
As this is my first AI experiment with
tensorflow
, I don't really know if this will work out, or if I should go with another strategy, therefore I need your help.  Normalize the headlines so

Selection column in a dataframe in pandas apply min function
I have ndataframe in a list
df=[df_1, df_2, df_3, ...., df_n]
Where df_n is a dataframe in pandas (python). df_n is a variable of my kerasmodel.
X_train=[df_1_1,df_2_1,...,df_n_1]
Where:
df_1_1 is the first dataframe of the list (the first variable) and the first columns of this dataframe, his dataframe has m columns.
Each column of this dataframe if this variable applies a different type of smoothing or filter.
I have 100 column in each dataframe and I want to select the combination of columns (of different dataframes), the X_train than have min value in the score of my model.
score = model.evaluate(X_test,Y_test)
X_test and Y_test are the last n occurrences of the selected columns.
There some library for selected this columns (neuronal networks, GA, colony ant, ...)?
How can I implement it?

Custom loss function in Keras, but it doesn't work
When I make a custom loss function as follow : (just the same as binary_crossentropy)
def custom_loss(y_true, y_pred): return losses.binary_crossentropy(y_true, y_pred)
And binary_crossentropy in keras/keras/losses.py is:
def binary_crossentropy(y_true, y_pred): return K.mean(K.binary_crossentropy(y_true, y_pred), axis=1)
When I run :
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics= ['accuracy']) model.compile(loss=losses.binary_crossentropy, optimizer=optimizer, metrics= ['accuracy'])
The output is correct.
ETA: 6:20  loss: 0.3697  acc: 0.8343
If I use my custom_loss:
model.compile(loss=custom_loss, optimizer=optimizer, metrics=['accuracy'])
The output is incorrect:
ETA: 10:49  loss: 0.7651  acc: 0.0694
We can see that the accuracy is incorrect. Why??

SQL concatenate rows into one field (DB2) With Filters
I am using DB2 V7r1 (so no listagg unfortunatly). I need to be able to have all the descriptions for the item(ItemNum) be in one string comma separated. I have a Query I found that kind of works but I can not filter the result by item number. It will work fine with the first 100 rows of data but if I try to filter out an item that is maybe 100,000 rows down the table, it takes forever.
*EDIT, I should add that this table contains 1,460,072 records, and one Item/Operation may have up to 60 Description entries, so if any one knows of a way to maybe prefilter the results or a more efficient way to do this i would appreciate it greatly
Here is my table: PARTS
ItemNum OpSequence DscNum Description A123 10 2 Desc Line 1 A123 10 4 Desc Line 2 A123 10 6 Desc Line 3 A123 20 2 Desc Line 1 A123 20 4 Desc Line 2 Z555 10 2 Desc Line 1 Z555 10 4 Desc Line 2
Here is the result I need (Need to filter by ItemNum and OpSequence)
ItemNum OpSequence Description A123 10 Desc Line 1, Desc Line 2, Desc Line 3
Here is the Query I used
with x (ItemNum, OpSequence, cnt, list, empno, len) as (select z.ItemNum, z.OpSequence, (select count(*) from PARTS y where y.ItemNum=z.ItemNum group by y.ItemNum), cast(z.Description as varchar(100)), rrn(z), 1 from PARTS z where z.ItemNum = 'A123' (HERE IS WHERE I AM TRYING TO FILTER) union all select x.ItemNum, x.OpSequence, x.cnt, strip(x.list) ', ' e.Description, rrn(e), x.len+1 from PARTS e, x where e.ItemNum = x.ItemNum and rrn(e) > x.empno ) select ItemNum,OpSequence, list from x where len=cnt

PHP  Combine Two Numbers Together as a Decimal Number
As the title suggest I have two whole numbers that I need to merge together but I need them to be split up by a . (decimal point). What I'm trying to achieve below...
$var1 = 12345; $var2 = 6789; //When combined together... $var3 = $var1 + "." + $var2; // var3 new value would be 12345.6789
That code above doesn't work since I am mixing strings and ints... Does anyone know how to achieve this result? Thanks!

Merge item from same list
I have this:
[('France', 'Euro'), ('Allemagne', 'Euro'), ('France', 'Dollar'), ('France', 'Livre'), ('Allemagne', 'Dollar')]
and i want:
[('France', 'Euro', 'Dollar', 'Livre'), ('Allemagne', 'Euro', 'Dollar')]
i have try with
if item[0][0] == item[1][0]: zip
but this is not what i want maybe with
for item in list:
i dont know how to compare item[0] with item[1] with fonction for

Keras and input shape to Conv1D issues
First off, I am very new to Neural Nets and Keras.
I am trying to create a simple Neural Network using Keras where the input is a time series and the output is another time series of same length (1 dimensional vectors).
I made dummy code to create random input and output time series using a Conv1D layer. The Conv1D layer then outputs 6 different time series (because I have 6 filters) and the next layer I define to add all 6 of those outputs into one which is the output to the entire network.
import numpy as np import tensorflow as tf from tensorflow.python.keras.models import Model from tensorflow.python.keras.layers import Conv1D, Input, Lambda def summation(x): y = tf.reduce_sum(x, 0) return y time_len = 100 # total length of time series num_filters = 6 # number of filters/outputs to Conv1D layer kernel_len = 10 # length of kernel (memory size of convolution) # create random input and output time series X = np.random.randn(time_len) Y = np.random.randn(time_len) # Create neural network architecture input_layer = Input(shape = X.shape) conv_layer = Conv1D(filters = num_filters, kernel_size = kernel_len, padding = 'same')(input_layer) summation_layer = Lambda(summation)(conv_layer) model = Model(inputs = input_layer, outputs = summation_layer) model.compile(loss = 'mse', optimizer = 'adam', metrics = ['mae']) model.fit(X,Y,epochs = 1, metrics = ['mae'])
The error I get is:
ValueError: Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 100]
Looking at the Keras documentation for Conv1D, the input shape is supposed to be a 3D tensor of shape (batch, steps, channels) which I don't understand if we are working with 1 dimensional data.
Can you explain the meaning of each of the items: batch, steps, and channels? And how should I shape my 1D vectors to allow my network to run?

Conv1D as dimensionality reduction for LSTM
I was hoping to use CNN as a dimensionality reduction for my LSTM layers.
I have a panel dataset as the following:
sequence of days = 5065 lags = 14 days (those are time series lags) features = 2767
Thus,
[5065, 14, 2767]
As you can see I have more than half as many features as data points, and I wanted to reduce that. Ideally, I wanted to feed my LSTM layers with compressed feature information with something like 32 features, hopefully in the following shape:
[5065, 14, 32]
However, when setting up the CNN, I understand that filters should be 32, but what about my kernel size? I'm not sure I'm doing the right thing.

Error when using tf.get_variable as alternativ for tf.Variable in Tensorflow
Hi I'm new to neural networks and I'm currently working on Tensoflow. First I did the MNIST tutorial which worked quite well. Now I wanted to deepen the whole by means of an own network for Cifar10 in Google Colab. For this purpose I wrote the following code:
def conv2d(input, size, inputDim, outputCount): with tf.variable_scope("conv2d"): ## > This area causes problems < ## ##########variant1 weight = tf.Variable(tf.truncated_normal([size, size, inputDim, outputCount], stddev=0.1),name="weight") bias = tf.Variable( tf.constant(0.1, shape=[outputCount]),name="bias") ##########variant2 weight = tf.get_variable("weight", tf.truncated_normal([size, size, inputDim, outputCount], stddev=0.1)) bias = tf.get_variable("bias", tf.constant(0.1, shape=[outputCount])) ################## conv = tf.nn.relu(tf.nn.conv2d(input, weight, strides=[1, 1, 1, 1], padding='SAME') + bias) return conv def maxPool(conv2d):.... def fullyConnect(input, inputSize, outputCount, relu): with tf.variable_scope("fullyConnect"): ## > This area causes problems < ## ##########variant1 weight = tf.Variable( tf.truncated_normal([inputSize, outputCount], stddev=0.1),name="weight") bias = tf.Variable( tf.constant(0.1, shape=[outputCount]),name="bias") ##########variant2 weight = tf.get_variable("weight", tf.truncated_normal([inputSize, outputCount], stddev=0.1)) bias = tf.get_variable("bias", tf.constant(0.1, shape=[outputCount])) ################## fullyIn = tf.reshape(input, [1, inputSize]) fullyCon = fullyIn if relu: fullyCon = tf.nn.relu(tf.matmul(fullyIn, weight) + bias) return fullyCon #Model Def. def getVGG16A(grafic,width,height,dim): with tf.name_scope("VGG16A"): img = tf.reshape(grafic, [1,width,height,dim]) with tf.name_scope("Layer1"): with tf.variable_scope("Layer1"): with tf.variable_scope("conv1"): l1_c = conv2d(img,3, dim, 64) with tf.variable_scope("mp1"): l1_mp = maxPool(l1_c) #32 > 16 with tf.name_scope("Layer2"): with tf.variable_scope("Layer2"): with tf.variable_scope("conv1"): l2_c = conv2d(l1_mp,3, 64, 128) with tf.variable_scope("mp1"): l2_mp = maxPool(l2_c) #16 > 8 with tf.name_scope("Layer6"): with tf.variable_scope("Layer6"): with tf.variable_scope("fully1"): L6_fc1 = fullyConnect(l2_mp, 8*8*128 , 1024, True) with tf.variable_scope("fully2"): L6_fc2 = fullyConnect(L6_fc1, 1024, 1024, True) keep_prob = tf.placeholder(tf.float32) drop = tf.nn.dropout(L6_fc2, keep_prob) with tf.variable_scope("fully3"): L6_fc3 = fullyConnect(drop,1024, 3, False) return L6_fc3, keep_prob x = tf.placeholder(tf.float32, [None, 3072]) #input y_ = tf.placeholder(tf.float32, [None, 3]) #output # Build the graph for the deep net y_conv, keep_prob = getVGG16A(x,32,32,3) #create Model cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) train_step = tf.train.AdamOptimizer(1e3).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for batch in getBatchData(prep_filter_dataBatch1,2): #a selfwritten method for custom batch return train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.8}) print('test accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
For the definition of the tensorflow variables I first used variant1 (tf.variable). This caused an overflow of the graphics memory after repeated execution. Then I used variant2 (tf.get_variable). If I have understood the documentation correctly, this should use already existing variables if they exist.
But as soon as I do this I get the following error message:
TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.
I've been looking the hole day, but I haven't found an explanation for this.
Now I hope that there is someone here who can explain to me why this is not possible, or where I can find further information. The error message is getting me nowhere. I don't want a solution because I want to and have to understand this, because I want to write my bachelor thesis in the field of CNN.
Why can I use tf.variable but not tf.get_variable which should do the same?
Thanks for the help, best regards, Pascal :)