2017-03-06 11 views
6

मैं tensorflow के लिए नया हूं और मैं टेन्सफोर्लो के पुराने संस्करण से नवीनतम (1.0) तक एक बिडरेक्शनल एलएसटीएम के लिए कुछ कोड अपडेट करने का प्रयास कर रहा हूं, लेकिन मैं यह त्रुटि प्राप्त करें:टेन्सफोर्लो: वैल्यूएरर: आकार रैंक 2 होना चाहिए लेकिन रैंक 3

Shape must be rank 2 but is rank 3 for 'MatMul_3' (op: 'MatMul') with input shapes: [100,?,400], [400,2].

त्रुटि pred_mod पर होती है।

_weights = { 
    # Hidden layer weights => 2*n_hidden because of foward + backward cells 
     'w_emb' : tf.Variable(0.2 * tf.random_uniform([max_features,FLAGS.embedding_dim], minval=-1.0, maxval=1.0, dtype=tf.float32),name='w_emb',trainable=False), 
     'c_emb' : tf.Variable(0.2 * tf.random_uniform([3,FLAGS.embedding_dim],minval=-1.0, maxval=1.0, dtype=tf.float32),name='c_emb',trainable=True), 
     't_emb' : tf.Variable(0.2 * tf.random_uniform([tag_voc_size,FLAGS.embedding_dim], minval=-1.0, maxval=1.0, dtype=tf.float32),name='t_emb',trainable=False), 
     'hidden_w': tf.Variable(tf.random_normal([FLAGS.embedding_dim, 2*FLAGS.num_hidden])), 
     'hidden_c': tf.Variable(tf.random_normal([FLAGS.embedding_dim, 2*FLAGS.num_hidden])), 
     'hidden_t': tf.Variable(tf.random_normal([FLAGS.embedding_dim, 2*FLAGS.num_hidden])), 
     'out_w': tf.Variable(tf.random_normal([2*FLAGS.num_hidden, FLAGS.num_classes]))} 

    _biases = { 
     'hidden_b': tf.Variable(tf.random_normal([2*FLAGS.num_hidden])), 
     'out_b': tf.Variable(tf.random_normal([FLAGS.num_classes]))} 


    #~ input PlaceHolders 
    seq_len = tf.placeholder(tf.int64,name="input_lr") 
    _W = tf.placeholder(tf.int32,name="input_w") 
    _C = tf.placeholder(tf.int32,name="input_c") 
    _T = tf.placeholder(tf.int32,name="input_t") 
    mask = tf.placeholder("float",name="input_mask") 

    # Tensorflow LSTM cell requires 2x n_hidden length (state & cell) 
    istate_fw = tf.placeholder("float", shape=[None, 2*FLAGS.num_hidden]) 
    istate_bw = tf.placeholder("float", shape=[None, 2*FLAGS.num_hidden]) 
    _Y = tf.placeholder("float", [None, FLAGS.num_classes]) 

    #~ transfortm into Embeddings 
    emb_x = tf.nn.embedding_lookup(_weights['w_emb'],_W) 
    emb_c = tf.nn.embedding_lookup(_weights['c_emb'],_C) 
    emb_t = tf.nn.embedding_lookup(_weights['t_emb'],_T) 

    _X = tf.matmul(emb_x, _weights['hidden_w']) + tf.matmul(emb_c, _weights['hidden_c']) + tf.matmul(emb_t, _weights['hidden_t']) + _biases['hidden_b'] 

    inputs = tf.split(_X, FLAGS.max_sent_length, axis=0, num=None, name='split') 

    lstmcell = tf.contrib.rnn.BasicLSTMCell(FLAGS.num_hidden, forget_bias=1.0, 
    state_is_tuple=False) 

    bilstm = tf.contrib.rnn.static_bidirectional_rnn(lstmcell, lstmcell, inputs, 
    sequence_length=seq_len, initial_state_fw=istate_fw, initial_state_bw=istate_bw) 


    pred_mod = [tf.matmul(item, _weights['out_w']) + _biases['out_b'] for item in bilstm] 

किसी भी मदद की सराहना की।

+0

आप किस गणना को करने की कोशिश कर रहे हैं? टेंसरफ्लो का 'tf.matmul()' अलग-अलग मैट्रिक्स गुणा या बैच मैट्रिक्स गुणाओं को निष्पादित कर सकता है, लेकिन इसे आकार के बारे में जानकारी चाहिए कि उसे यह पता होना चाहिए कि उसे कौन सा करना चाहिए। – mrry

उत्तर

2

भविष्य में इस समस्या का सामना करने वाले किसी भी व्यक्ति के लिए, से ऊपर स्निपेट का उपयोग नहीं किया जाना चाहिए।

tf.contrib.rnn.static_bidirectional_rnn v1.1 प्रलेखन से:

Returns:

A tuple (outputs, output_state_fw, output_state_bw) where: outputs is a length T list of outputs (one for each input), which are depth-concatenated forward and backward outputs. output_state_fw is the final state of the forward rnn. output_state_bw is the final state of the backward rnn.

सूची समझ से ऊपर की उम्मीद है LSTM outputs, और उन प्राप्त करने के लिए सही तरीका यह है:

outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(lstmcell, lstmcell, ...) 
pred_mod = [tf.matmul(item, _weights['out_w']) + _biases['out_b'] 
      for item in outputs] 

यह काम करेंगे, क्योंकि प्रत्येक itemoutputs में [batch_size, 2 * num_hidden] आकार है और tf.matmul() द्वारा वजन के साथ गुणा किया जा सकता है।


ऐड-ऑन: tensorflow v1.2 से +, उपयोग करने के लिए सिफारिश की समारोह एक और पैकेज में है: tf.nn.static_bidirectional_rnn। लौटा टेंसर एक जैसा है, इसलिए कोड अधिक नहीं बदलता है:

outputs, _, _ = tf.nn.static_bidirectional_rnn(lstmcell, lstmcell, ...) 
pred_mod = [tf.matmul(item, _weights['out_w']) + _biases['out_b'] 
      for item in outputs] 
संबंधित मुद्दे