2017-11-10 5 views
0

キューを使用して変換しようとしている動作テンソルフローモデルがあります。それは最高の機能ではないかもしれませんが、機能します。feed_dictからキューに変換すると損失が増えます

データは、フォーム形式[{'y1':1、 'y2':0、 'y3':1]のTF以外の処理パイプラインから 'rows'というリスト(dict 、 'y4':0、 'x1':... 'x1182':0}](SPECIAL_FIELD_CHARは 'xN'データから計算される 'y'です。 features_outputs()はxsとysを['y1'、 'y2'、 'y3'、 'y4']と['x1'、...、 'x1182']として返します。アイデアは、xsがysを決定するということです。 xsの行ごとに計算される4つの独立したyがあります。

def train_rows(initial_weights, weights_filename, rows): 
    (features, outputs) = features_outputs(rows[0].keys()) 

    x_true = [ [float(row[feature]) for feature in features] for row in rows] 
    try: 
     y_true = [ [float(row[output]) for output in outputs] for row in rows ] 
    except Exception as e: 
     print [row[output] for output in outputs], e 

    w_true = np.random.rand(len(features), 1) # init weights 
    b_true = np.random.rand(1) # init bias 

    x_in = tf.placeholder(tf.float32, [None, len(features)], "x_in") 
    if initial_weights is None: 
     w = tf.Variable(tf.random_normal((len(features), len(outputs))), name="w") 
     b = tf.Variable(tf.constant(0.1, shape=[len(outputs)]), name="b") 
    else: 
     w = tf.Variable(weights['w'], name="w") 
     b = tf.Variable(weights['b'], name="b") 

    h = tf.add(tf.matmul(x_in, w), b, name="h") 
    y_in = tf.placeholder(tf.float32, [None, len(outputs)], "y_in") 
    loss_op = tf.reduce_mean(tf.square(tf.subtract(y_in, h)), name="loss") 
    #train_op = tf.train.AdamOptimizer(0.01).minimize(loss_op) 
    train_op = tf.train.GradientDescentOptimizer(0.3).minimize(loss_op) 

    with tf.Session() as sess: 
     sess.run(tf.global_variables_initializer()) 
     last_error = 1.7976931348623157e+308 
     this_error = 1.7976931348623157e+307 
     diff = 1 
     iteration = initial_weights['iteration'] if initial_weights is not None and 'iteration' in initial_weights else 0 
     while diff > 0: 
      iteration += 1 
      last_error = this_error 
      for step in range(1000): 
       sess.run(train_op, feed_dict={ 
        x_in: x_true, 
        y_in: y_true 
       }) 

      w_computed = sess.run(w) 
      b_computed = sess.run(b) 

      pred = tf.add(tf.matmul(x_in, w), b) 

      results = sess.run(pred, feed_dict={x_in: x_true}) 
      error = tf.losses.mean_squared_error(y_true, results) 
      this_error = float(error.eval()) 
      (diff, locs) = compare(y_true, results) 
      if locs < 50: 
       print "iteration:", iteration, "error:",this_error, "diff:", diff, "locs:", locs 

これにより、収束するモデルが生成されます。しかし、急速にそれはないキューベースのバージョン、およびエラーが増加して:( 1. tf.transposeを追加します。

def multithreaded_train_rows(initial_weights, weights_filename, rows): 
    (features, outputs) = features_outputs(rows[0].keys()) 

    x_true = np.array([ [float(row[feature]) for feature in features] for row in rows]) 
    y_true = np.array([ [float(row[output]) for output in outputs] for row in rows ]) 


    #queue 
    q = tf.FIFOQueue(capacity=len(rows), dtypes=tf.float32) 
    #enq_op = q.enqueue_many(x_true) 
    enq_op = q.enqueue_many(np.array([ [float(row[f]) for f in sorted(row.keys())] for row in rows] )) 
    qr = tf.train.QueueRunner(q, [enq_op] * 1) 
    tf.train.add_queue_runner(qr) 

    keys = sorted(row.keys()) 
    x_indices = np.array([[i] for i in range(len(keys)) if not keys[i].startswith(SPECIAL_FIELD_CHAR)]) 
    y_indices = np.array([[i] for i in range(len(keys)) if  keys[i].startswith(SPECIAL_FIELD_CHAR)]) 

    input = q.dequeue() 

    x_in = tf.transpose(tf.gather(input, x_indices)) 
    y_in = tf.gather(input, y_indices) 

    if initial_weights is None: 
     print 'Creating weights', len(x_indices), len(y_indices) 
     w = tf.Variable(tf.random_normal((len(x_indices), len(y_indices))), name="w") 
     b = tf.Variable(tf.constant(0.1, shape=[len(y_indices)]), name="b") 
    else: 
     print 'Using supplied weights', len(weights['w']), len(weights['w'][0]) 
     w = tf.Variable(weights['w'], name="w") 
     b = tf.Variable(weights['b'], name="b") 

    y = tf.add(tf.matmul(x_in, w), b, name="y") 

    loss_op = tf.reduce_mean(tf.squared_difference(y_in, y), name="loss") 
    train_op = tf.train.GradientDescentOptimizer(0.3).minimize(loss_op) 

    print 'Starting session' 
    with tf.Session() as sess: 
     sess.run(tf.global_variables_initializer()) 
     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(coord=coord) 
     last_error = 1.7976931348623157e+308 
     this_error = 1.7976931348623157e+307 
     diff = 1 
     iteration = initial_weights['iteration'] if initial_weights is not None and 'iteration' in initial_weights else 0 
     while diff > 0: 
      iteration += 1 
      last_error = this_error 
      for step in range(100): 
       sess.run([train_op, loss_op]) 

      w_computed = sess.run(w) 
      b_computed = sess.run(b) 

      pred = tf.add(tf.matmul(x_in, w), b) 

      results = sess.run(y, feed_dict={x_in: x_true}) 
      error = tf.losses.mean_squared_error(y_true, results) 
      this_error = float(error.eval()) 

      (diff, locs) = compare(y_true, results) 
      if locs < 50: 
       print "iteration:", iteration, "error:",this_error, "diff:", diff, "locs:", locs 

     coord.request_stop() 
     coord.join(threads) 

彼らは同じであることを意味しているが、私はいくつかのことを変更しなければなりませんでした)をmatmul()のx_inに置き換えます。 2. xsとysの行全体をキューに入れ、tf.gather()を使用して分割します。

私はマインと一致するサンプルをたくさん検索しましたが、キューを再起動して最初からトレーニングを続ける方法に関するドキュメントは見つかりません。それは永遠に訓練されているように見えるでしょう(誰がキューを補充しているのですか?)それは決して止まらないでしょう。

しかし、私はまったく同じデータが与えられている理由がわからないのですが、最初のデータは収束し、2番目のデータは収束しません。

答えて

0
  • ギャザーマーシャリングは必要ありません。
  • がy_trueに対してYを評価し、その後、唯一の入力(x_true)をキューないY_IN

    loss_op = tf.reduce_mean(tf.square(y - y_true), name="loss") 
    
関連する問題