title: Initialize TensorFlow CNN model with Numpy weight matrices
body: <p>I am working on manually converting a pretrained matconvnet model to a tensorflow model. I have pulled the weights/biases from the matconvnet model mat file using scipy.io and obtained numpy matrices for the weights and biases. </p>
  
  <p><strong>Code snippets</strong> where <code>data</code> is a dictionary returned from scipy.io:</p>
  
  <pre><code>for i in data['net2']['layers']:
      if i.type == 'conv':
          model.append({'weights': i.weights[0], 'bias': i.weights[1], 'stride': i.stride, 'padding': i.pad, 'momentum': i.momentum,'lr': i.learningRate,'weight_decay': i.weightDecay})
  </code></pre>
  
  <p>...</p>
  
  <pre><code>weights = {
      'wc1': tf.Variable(model[0]['weights']), 
      'wc2': tf.Variable(model[2]['weights']),
      'wc3': tf.Variable(model[4]['weights']),
      'wc4': tf.Variable(model[6]['weights'])
  }
  </code></pre>
  
  <p>...</p>
  
  <p>Where <code>model[0]['weights']</code> are the 4x4x60 numpy matrices pulled from matconvnet model for for layer, for example. And this is how I define the place holder for the 9x9 inputs. </p>
  
  <pre><code>X = tf.placeholder(tf.float32, [None, 9, 9]) #also tried with [None, 81] with a tf.reshape, [None, 9, 9, 1]
  </code></pre>
  
  <p><strong>Current Issue</strong>: I cannot get ranks to match up. I consistently getValueError: </p>
  
  <pre><code>ValueError: Shape must be rank 4 but is rank 3 for 'Conv2D' (op: 'Conv2D') with input shapes: [?,9,9], [4,4,60]  
  </code></pre>
  
  <p><strong>Summary</strong></p>
  
  <ul>
  <li>Is it possible to explicitly define a tensorflow model's weights from numpy arrays?</li>
  <li>Why is the rank for my weight matrices 4? Should my numpy array be something more like [?, 4, 4, 60], and can I make it that way? </li>
  </ul>
  
  <p><strong>Unsuccessfully Attempted:</strong></p>
  
  <ul>
  <li>Rotating numpy matrices: I know that matlab and python have different indexing, (0 based indexing vs 1 based, and row major vs column major). Even though I believe I have converted everything appropriately, I still have experimented using libraries like np.rot90() changing 4x4x60 array to 60x4x4.   </li>
  <li>Using tf.reshape: I have attempted to use tf.reshape on the weights after wrapping them with a tf.Variable wrapper, but I get Variable has no attribute 'reshape'</li>
  </ul>
  
  <p><strong>NOTE</strong>:
  Please note, I am aware that there are a number of scripts to go from matconvnet to caffe, and caffe to tensorflow (as described here, for example, <a href="https://github.com/vlfeat/matconvnet/issues/1021" rel="nofollow noreferrer">https://github.com/vlfeat/matconvnet/issues/1021</a>). My question is related to tensorflow weight initialization options:</p>
  
  <ul>
  <li><a href="https://github.com/zoharby/matconvnet/blob/master/utils/convert_matconvnet_caffe.m" rel="nofollow noreferrer">https://github.com/zoharby/matconvnet/blob/master/utils/convert_matconvnet_caffe.m</a></li>
  <li><a href="https://github.com/ethereon/caffe-tensorflow" rel="nofollow noreferrer">https://github.com/ethereon/caffe-tensorflow</a></li>
  </ul>