2016-11-25 7 views
1

Tensorflowでニューラルネットワークを作成しようとしていますが、単純な単純な配列/リスト入力を行うネットワークの作り方を考えていません。私はTensorflowのチュートリアルに続いて試してみましたが、ほとんどがmnistの手書きデータセットを使用しています。Tensorflow - 単純な配列/リスト入力を処理するニューラルネットワークを作成する

たとえば、このような単純なXとYのデータが必要です。

X = np.array(([3, 5], [5, 1], [10, 2]), dtype=float) 
Y = np.array(([75], [82], [93]), dtype=float) 

ここで、Xは、試験のために勉強した時間と睡眠時間で構成されています。 そしてYは、それらの試験で受けた対応する成績で構成されています。すべてのネットワークは、2つの入力ノード、3〜5の隠れノード、および1つの出力ノードで構成されなければならない。

私は従うことをしようとしてきた例は https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py

答えて

0

以下の内容は、スタックオーバーフローのドキュメント (archived here)から「Tensorflowを始めGeting」からです。著作権2017 by EngineeroMaciej LipinskiNicolasdaolikerSteven、およびMad Matts; はCC BY-SA 3.0でライセンスされています。フルスタックオーバーフロー ドキュメントのコンテンツのアーカイブはこの 例は、そのトピックIDでインデックス化されている、archive.orgで見つけることができます:856、例として:4069.

Tensorflowは単なる以上のものです深い学習の枠組み。これは、一般的な数学的操作を並列かつ分散して実行するための一般的な計算フレームワークです。その一例を以下に説明する。

線形回帰

一般的に利用し、計算にかなり単純である基本的な統計の例では、データセットの行を適合されます。テンソルフローで行う方法は、コードとコメントで後述します。 (TensorFlow)スクリプトの

主な手順は次のとおり

  1. はプレースホルダ(x_phy_ph)および変数(Wb
  2. 初期化演算子(init
  3. 宣言操作を定義する宣言しますプレースホルダーと変数(y_predlosstrain_op
  4. セッションを作成するsess
  5. 初期化演算子を実行してください(sess.run(init)
  6. sess.run([train_op, loss], feed_dict={x_ph: x, y_ph: y})

グラフの構築は、Python TensorFlow APIを使用して行われます(C++ TensorFlow APIを使用して行うこともできます)。グラフを実行すると、低レベルのC++ルーチンが呼び出されます。

''' 
function: create a linear model which try to fit the line 
      y = x + 2 using SGD optimizer to minimize 
      root-mean-square(RMS) loss function 

''' 
import tensorflow as tf 
import numpy as np 

# number of epoch 
num_epoch = 100 

# training data x and label y 
x = np.array([0., 1., 2., 3.], dtype=np.float32) 
y = np.array([2., 3., 4., 5.], dtype=np.float32) 

# convert x and y to 4x1 matrix 
x = np.reshape(x, [4, 1]) 
y = np.reshape(y, [4, 1]) 

# test set(using a little trick) 
x_test = x + 0.5 
y_test = y + 0.5 

# This part of the script builds the TensorFlow graph using the Python API 

# First declare placeholders for input x and label y 
# Placeholders are TensorFlow variables requiring to be explicitly fed by some 
# input data 
x_ph = tf.placeholder(tf.float32, shape=[None, 1]) 
y_ph = tf.placeholder(tf.float32, shape=[None, 1]) 

# Variables (if not specified) will be learnt as the GradientDescentOptimizer 
# is run 
# Declare weight variable initialized using a truncated_normal law 
W = tf.Variable(tf.truncated_normal([1, 1], stddev=0.1)) 
# Declare bias variable initialized to a constant 0.1 
b = tf.Variable(tf.constant(0.1, shape=[1])) 

# Initialize variables just declared 
init = tf.initialize_all_variables() 

# In this part of the script, we build operators storing operations 
# on the previous variables and placeholders. 
# model: y = w * x + b 
y_pred = x_ph * W + b 

# loss function 
loss = tf.mul(tf.reduce_mean(tf.square(tf.sub(y_pred, y_ph))), 1./2) 
# create training graph 
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss) 

# This part of the script runs the TensorFlow graph (variables and operations 
# operators) just built. 
with tf.Session() as sess: 
    # initialize all the variables by running the initializer operator 
    sess.run(init) 
    for epoch in xrange(num_epoch): 
     # Run sequentially the train_op and loss operators with 
     # x_ph and y_ph placeholders fed by variables x and y 
     _, loss_val = sess.run([train_op, loss], feed_dict={x_ph: x, y_ph: y}) 
     print('epoch %d: loss is %.4f' % (epoch, loss_val)) 

    # see what model do in the test set 
    # by evaluating the y_pred operator using the x_test data 
    test_val = sess.run(y_pred, feed_dict={x_ph: x_test}) 
    print('ground truth y is: %s' % y_test.flatten()) 
    print('predict y is  : %s' % test_val.flatten()) 
関連する問題