Looking for an example on how to use ConvLSTM2D layer in tensorflow
I'm trying to implement a convolution LSTM model and ConvLSTM2D layer seems to be what I'm looking for, but couldn't find any example on how to use it.
This is the documentation in tensorflow website: https://www.tensorflow.org/versions/master/api_docs/python/tf/contrib/keras/layers/ConvLSTM2D
See also questions close to this topic

Tensorflow: TFRecord with string feature
I have a basic question about TFRecord. All of the examples that I have seen do not over this topic as they are all using images in their example. I have a dataset that I want to train where the starting input data from a CSV includes some string categorical features.
When I use a canned estimator I can create a a tf.feature_column like so for a categorical.
resolved = tf.feature_column.categorical_column_with_vocabulary_list('resolved', ['Yes', 'No', 'Unsure/still waiting'])
Which is great because I then don't have to manually build dummy columns. However, now I'm in a world where I want to setup a multithreaded scalable ingestion process and I want to use the TFRecord serialization capability. So here are my questions:
 Given I start with categorical feature columns do I have to 'dummy' those cat features from my original data source out into INT columns prior to persisting to a TFRecord or is there some available capability out there to get those stored without dealing with dummying?

Using Datasets to consume Numpy arrays
I'm trying to use Numpy arrays within a graph, feeding in the data using a Dataset.
I've read through this, but can't quite make sense of how I should feed placeholder arrays within a Dataset.
If we take a simple example, I start with:
A = np.arange(4) B = np.arange(10, 14) a = tf.placeholder(tf.float32, [None]) b = tf.placeholder(tf.float32, [None]) c = tf.add(a, b) with tf.Session() as sess: for i in range(10): x = sess.run(c, feed_dict={a: A, b:B}) print(i, x)
Then I attempt to modify it to use a Dataset as follows:
A = np.arange(4) B = np.arange(10, 14) a = tf.placeholder(tf.int32, A.shape) b = tf.placeholder(tf.int32, B.shape) c = tf.add(a, b) dataset = tf.data.Dataset.from_tensors((a, b)) iterator = dataset.make_initializable_iterator() with tf.Session() as sess3: sess3.run(tf.global_variables_initializer()) sess3.run(iterator.initializer, feed_dict={a: A, b: B}) for i in range(10): x = sess3.run(c) print(i, x)
If I run this I get 'InvalidArgumentError: You must feed a value for placeholder tensor ...'
The code until the for loop mimics the example here, but I don't get how I can then employ the placeholders a & b without supplying a feed_dict to every call to sess3.run(c) [which would be expensive]. I suspect I have to somehow use the iterator, but I don't understand how.

Model doesn't learn anything
I am working on kaggle.com competition called Planet:Amazon from space.
I use keras VGG19 Model without top layers followed by some fully connected layers as below:
fc1 > 512*1024 activation=relu
fc2 > 1024*1024 activation=relu
fc3 > 1024*1024 activation=relu
fc4 > 1024*13 activation=sigmoid
My loss is mean of sigmoid_cross_entropy_with_logits and my fully_connected definition is :def fully_connected(self, x, W, b, name): return tf.nn.bias_add(tf.matmul(x, W), b, name=name)
Optimizer is Adam and the learning rate is 0.001
I freezed vgg and trained classifier.The lose value started as 0.89 and goes down to 0.69 but it stops and nothing is changing and the model doesn't learn anything.In meanwhile the algorithm updated the weights to predict ~[0 0 0 0 0 0 0 0 0 0 0 0 0] for any sample.
My functions are:def build(self, vgg_tensor): final_conv_shape = vgg_tensor.shape # conv = tf.reshape(vgg_tensor, # [1, int(final_conv_shape[1]) * int(final_conv_shape[2]) * int(final_conv_shape[3])]) vgg_tensor = tf.contrib.layers.batch_norm(vgg_tensor) self.fc1 = self.fully_connected(vgg_tensor, self.fc_weights['wfc1'], self.fc_weights['bfc1'], 'fc1') self.batch1 = tf.contrib.layers.batch_norm(self.fc1) self.relu1 = tf.nn.relu(self.batch1, name='relu1') self.fc2 = self.fully_connected(self.relu1, self.fc_weights['wfc2'], self.fc_weights['bfc2'], 'fc2') self.batch2 = tf.contrib.layers.batch_norm(self.fc2) self.relu2 = tf.nn.relu(self.batch2, name='relu2') self.fc3 = self.fully_connected(self.relu2, self.fc_weights['wfc3'], self.fc_weights['bfc3'], 'fc3') self.batch3 = tf.contrib.layers.batch_norm(self.fc3) self.relu3 = tf.nn.relu(self.batch3, name='relu3') self.out = self.fully_connected(self.relu3, self.fc_weights['wout'], self.fc_weights['bout'], 'out') self.predict = tf.nn.sigmoid(self.out, name='predict')
def declare_error(self, labels): self.each_error = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=self.predict) self.error = tf.reduce_mean(self.each_error) return tf.summary.scalar('cost', self.error)
def declare_optimizer(self, lr, variables): self.optimizer = tf.train.AdamOptimizer(lr, name='opt').minimize(self.error, var_list=variables)
Any suggestions about my issue? 
How is logsumexp pooling layer used to create bounding boxes with just label?
There is a solution to weak supervision problem that involves using a logsumexp pooling layer with units of the type: y = 1/β log (∑i exp(βzi)). What does the network architecture look like and how is a layer of these pooling unit used?

After object detection in an image, refining the object bounding box if the object or camera is moved slightly
This is the situation I'm running into:
 User indicates where an object is on an image by specifying the bounding box.
One of two things can happen:
2A/ The object is moved.
2B/ The camera is moved.
I'd now like to update the bounding box of the object. for both (2A) or (2B), the changes will be minor  so the camera might move a few centimeters or the object might move a meter. What are the best approaches to calculate where the new bounding box is?
My current naive approach is applying a window search technique that looks at % match (passing the original object image over the new large image). If % match meets a threshold, the bounding box is updated.

Constant Validation loss
I have a model(CNN with custom loss) that performs very well with data completely disconnetted from training and validation ones. During training phase training loss is going down and validation loss is almost constant. We double checked for some bugs, but training data, validation and especially final test are completely separated and they are different. No correlation between them. So, how is possible to see a bad validation loss curve but performs so well with so many tests?

LSTM equivalent of return_sequence = True in newer version of Keras
I am using tensorflow as backend to keras. In one of the tutorials for LSTM, it says
regressor.add(LSTM(units = 50, return_sequence = True, input_shape = Xtrain))
Looking at the new LSTM syntax it says
keras.layers.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', unit_forget_bias=True, kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, implementation=1, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False)
Can someone please provide the equivalent of the above code in newer version?

Tensorflow encoderdecoder dynamic lstm with variable length inputs and outputs
I am implementing an encoderdecoder LSTM network that should reconstruct the input sequence.
Currently, I am getting an error:
ValueError: setting an array element with a sequence.
I think it happens because I feed variable length inputs as part of
feed_dict
.This is the function responsible for constructing feed dictionary:
def next_feed(batch_index): encoder_inputs_ = train_X[batch_index:batch_index + train_batch_size] # encoder input = [w1, w2, ...] encoder_inputs_length_ = seq_lengths[batch_index: batch_index + train_batch_size] decoder_targets_ = [] for i in range(encoder_inputs_.shape[0]): l = seq_lengths[batch_index: batch_index + train_batch_size][i] trimmed = np.array(encoder_inputs_[i, 0:l+1]) decoder_targets_.append(trimmed) decoder_targets_ = np.array(decoder_targets_) return { encoder_inputs: encoder_inputs_, decoder_targets: decoder_targets_, encoder_inputs_length: encoder_inputs_length_ }
If I do not trim the
decoder_targets
that I get another error, because dimensions oflogits
andlabels
do not match:InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[16,7810] labels_size=[550,7810]
Question: how to handle this error? Ideally I would like to stick to trimmed
decoder_targets
.EDIT: Is padding the only way to deal with such cases?

Keras LSTM good accuracy during training, wrong predictions
I have a simulated environment that consists of a [0.0  1.0]x[0.0  1.0] square with 2 objects inside that have their respectively coordinates (x1, y1) and (x2, y2). I also have a gripper state that represent if the simulated robotic gripper is full or empty.
The goal is to swap the two objects. So always pick(obj1) first, place it in a temp location, pick(Obj2) place it in the original Obj1 position, Pick(Obj1) again and place it in Obj2 original position.
There is a chance that a Pick or Place fails. In that case the network should be able to recognize that through the observed environment and act accordingly. My input is a sequence with 5 dimensions per time step: where gripper_status is either 0 or 1.
When gripper_status=1 and, for example, x1=0 and y1=0 it means that Object 1 has been grasped.
The Output is a sequence with 6 dimensions per time step: . One of Pick(O1), Pick(O2), Place or End should be selected as next move. When Place is at the highest values I also use (x_target, y_target) to place the object.
I am generating an artificial dataset with a random error probability during pick and places. The problem is I get good overall accuracy during training and evaluation (>90%) but the network behaves in an incorrect manner when I try it through predict() for each timestep. In particular it seems it can predict the Pick(Obj1) and Place(rand_x, rand_y) but when it should Pick(Obj2) it just Pick(Obj1) again.
Also I am padding all sequences shorter than a fixed number with 1.0 so that I have sequences of the same length (correct?) I am using Masking layer input because I was afraid that matching a lot of 0s could drive up the accuracy%.
Here's part of the code:
model = Sequential() model.add(Masking(mask_value= 1.0, input_shape=(None, 5))) model.add(LSTM(units=30, return_sequences=True)) model.add(TimeDistributed(Dense(6))) model.compile(loss="mean_squared_error", optimizer='adam', metrics=['accuracy'] ) model.fit(x_train, y_train, epochs=3, batch_size=8, validation_split=0.2)
A training example (Without pcik or place errors):
In: [0.011, 0.053, 0.239, 0.670, 0.0] Out: [1.0, 0.0, 0.0, 0.0 , 0.0 , 0.0] // Pick Object 1 In: [0.0 , 0.0 , 0.239, 0.670, 1.0] Out: [0.0, 0.0, 1.0, 0.143, 0.820, 0.0] // Place Obj in X=0.143, Y=0.82 In: [0.143, 0.820, 0.239, 0.670, 0.0] Out: [0.0, 1.0, 0.0, 0.0 , 0.0 , 0.0] // Pick Object 2 In: [0.143, 0.820, 0.0 , 0.0 , 1.0] Out: [0.0, 0.0, 1.0, 0.011, 0.053, 0.0] // Place Obj in X=0.011, Y=0.053 In: [0.143, 0.820, 0.011, 0.053, 0.0] Out: [1.0, 0.0, 0.0, 0.0 , 0.0 , 0.0] // Pick Object 1 In: [0.0 , 0.0 , 0.011, 0.053, 1.0] // Place Obj in X=0.011, Y=0.053 Out: [0.0, 0.0, 1.0, 0.239, 0.670, 0.0] In: [0.239, 0.670, 0.011, 0.053, 0.0] Out: [0.0, 0.0, 0.0, 0.0 , 0.0 , 1.0] // END

Building a Neural Network to play 2048  General approach guidance
Background
I still have a lot to learn about machine learning and am trying to build a convolution neural network that beats the game 2048 as exercise to gain experience. I am working on this project solo so I wanted to make sure my approach is sensible so I don't spend significant time on an approach with that could have been implemented better with a bit of guidance.Training Data
I am leveraging a highly optimized expectimax algorithm (code  description) in order to generate training data. I want to train the network on ~500k games. For each board state encountered during expectimax games and for direction one can swipe on that board I want to save:
* The board state * Action (swipe direction) * Heuristic of the board after being swiped in a particular directionEach board will generate data that would look like [board,r,7], [board,u,9], [board,l,7], [board,d,7]. I would also save action/heuristic information for all the various adaptations of the board that are equivalent when it comes to heuristics (e.g., transpositions, rotations, etc.).
Training
The first level of the CNN will have two parts convolving in parallel. Each part will have 64 filters of size (1,2) and (2,1). Just like in the first layer, both outputs will then have two convolutions applied of size (1,2) and (2,1) respectively. This output will be flattened and then there will be 4 layers of 128 nodes without dropout = 0.4. The final layer will be comprised of 4 nodes representing the four different actions (i.e., the 4 directions one can swiped). The network will then be trained over the ~500k games played by the expectimax algorithm. After training the neural network should predict which direction is best to swipe in.Discussion
I have been working in vacuum. Have I made any glaring errors? Does my logic make sense? Any and all guidance is appreciated. Thank you. 
Tensorflow: Attempting to use uninitialized value beta1_power
I got the following error when I try to run the code at the end of the post. But it is not clear to me what is wrong with my code. Could anybody let me know the tricks in debugging a tensorflow program?
$ ./main.py Extracting /tmp/data/trainimagesidx3ubyte.gz Extracting /tmp/data/trainlabelsidx1ubyte.gz Extracting /tmp/data/t10kimagesidx3ubyte.gz Extracting /tmp/data/t10klabelsidx1ubyte.gz 20171211 22:53:16.061163: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA Traceback (most recent call last): File "./main.py", line 55, in <module> sess.run(opt, feed_dict={x: batch_x, y: batch_y}) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/client/session.py", line 889, in run run_metadata_ptr) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/client/session.py", line 1120, in _run feed_dict_tensor, options, run_metadata) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/client/session.py", line 1317, in _do_run options, run_metadata) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/client/session.py", line 1336, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value beta1_power [[Node: beta1_power/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](beta1_power)]] Caused by op u'beta1_power/read', defined at: File "./main.py", line 46, in <module> opt=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/training/optimizer.py", line 353, in minimize name=name) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/training/optimizer.py", line 474, in apply_gradients self._create_slots([_get_variable_for(v) for v in var_list]) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/training/adam.py", line 130, in _create_slots trainable=False) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/ops/variable_scope.py", line 1927, in variable caching_device=caching_device, name=name, dtype=dtype) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/ops/variables.py", line 213, in __init__ constraint=constraint) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/ops/variables.py", line 356, in _init_from_args self._snapshot = array_ops.identity(self._variable, name="read") File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/ops/array_ops.py", line 125, in identity return gen_array_ops.identity(input, name=name) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/ops/gen_array_ops.py", line 2071, in identity "Identity", input=input, name=name) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/framework/ops.py", line 2956, in create_op op_def=op_def) File "/usr/local/lib/python2.7/sitepackages/tensorflow/python/framework/ops.py", line 1470, in __init__ self._traceback = self._graph._extract_stack() # pylint: disable=protectedaccess FailedPreconditionError (see above for traceback): Attempting to use uninitialized value beta1_power [[Node: beta1_power/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](beta1_power)]]
The code is here. It uses LSTM.
#!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=1 fileencoding=utf8: import tensorflow as tf from tensorflow.contrib import rnn #import mnist dataset from tensorflow.examples.tutorials.mnist import input_data mnist=input_data.read_data_sets("/tmp/data/", one_hot=True) learning_rate=0.001 #defining placeholders #input image placeholder time_steps=28 n_input=28 x=tf.placeholder("float", [None, time_steps, n_input]) #processing the input tensor from [batch_size,n_steps,n_input] to "time_steps" number of [batch_size,n_input] tensors input=tf.unstack(x, time_steps, 1) #defining the network num_units=128 lstm_layer = rnn.BasicLSTMCell(num_units, forget_bias=1) outputs,_ = rnn.static_rnn(lstm_layer, input, dtype="float32") #weights and biases of appropriate shape to accomplish above task n_classes=10 out_weights=tf.Variable(tf.random_normal([num_units, n_classes])) out_bias=tf.Variable(tf.random_normal([n_classes])) #converting last output of dimension [batch_size,num_units] to [batch_size,n_classes] by out_weight multiplication prediction=tf.matmul(outputs[1], out_weights) + out_bias y=tf.placeholder("float", [None, n_classes]) loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)) #optimization #model evaluation correct_prediction=tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) accuracy=tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #initialize variables init=tf.global_variables_initializer() batch_size=128 opt=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) with tf.Session() as sess: sess.run(init) iter=1 while iter<800: batch_x, batch_y = mnist.train.next_batch(batch_size=batch_size) batch_x=batch_x.reshape((batch_size, time_steps, n_input)) sess.run(opt, feed_dict={x: batch_x, y: batch_y}) if iter %10==0: acc=sess.run(accuracy,feed_dict={x:batch_x,y:batch_y}) los=sess.run(loss,feed_dict={x:batch_x,y:batch_y}) print("For iter ",iter) print("Accuracy ",acc) print("Loss ",los) print("__________________") iter=iter+1 #calculating test accuracy test_data = mnist.test.images[:128].reshape((1, time_steps, n_input)) test_label = mnist.test.labels[:128] print("Testing Accuracy:", sess.run(accuracy, feed_dict={x: test_data, y: test_label}))

How can we create a Multinomial matrix without using several for loops?
I want to create an algorithm that takes two arguments (
N = Number of parameters
,PO = polynomial order
) and returns multinomial matrix as shown in the linked file. examplesI have created a simple algorithm that takes PO as argument and gives me the multinomial matrix for constant value of N. For example for N = 4 the algorithm looks as show below. If I would like to create a matrix for N = other than 4, I have to change the number of for loops, in other words N = number of for loops. What must be modified in this algorithm such that we avoid writing for loops manually for different N. How to create a algorithm in which N can be a variable ?
PO = 4 a = 0 out = np.zeros((1,N)) for l in xrange(0, PO+1): for m in xrange(0, PO+1l): for k in range(0,PO+1lm): for j in range(0,PO+1lmk): a += 1 out = np.vstack([out, np.zeros(N)]) out[a1,0] = l out[a1,1] = m out[a1,2] = k out[a1,3] = j out = out[:1] print out