tf.group、tf.tuple、tf.control_dependencies、randomForest



Tf Group Tf Tuple Tf



tf.group

tf.group( *inputs, **kwargs )

この機能は、パラメーター内の操作をグループとして扱い、これらの操作を操作として扱うことです。
グループパラメータは操作であり、リストではありません(これが、入力の前に*を追加する理由です。注は1つずつである必要があります)
リターン:リターンはopです

tf.tuple

tf.tuple( tensors, name=None, control_inputs=None )

パラメータの説明:
テンソル:リストです
name :(オプション)この操作の名前を宣言します
control_inputs:戻る前に行う必要があること
return:テンソルのさまざまなテンソルの組み合わせのリストを返します。



tf.groupおよびtf.tupleのコード例

import tensorflow as tf w = tf.Variable(1) mul = tf.multiply(w, 2) add = tf.add(w, 2) group = tf.group(mul, add) #Note here is one by one tuple = tf.tuple([mul, add])#Note here is a list sess=tf.Session() sess.run(tf.global_variables_initializer()) print(sess.run([mul,add,group,w])) print(sess.run(tuple))

出力は次のとおりです。

[2, 3, None, 1] [2, 3]

tf.identity()

これは割り当て操作です。 これは操作(操作)であることに注意してください 。 y = tf.identity(x)効果はy = xと同等ですが、前者は操作であり、opはグラフのopであり、後者はそうではありません。



tf.control_dependencies():

tf.control_dependencies()設計は、フローグラフを制御し、グラフ内のいくつかの計算にシーケンスを割り当てるために使用されます。
たとえば、パラメータの更新された値を取得したい場合は、次のようにコードを整理できます。

何か言って:

control_dependenciesの意味は次のとおりです。操作を使用して操作を実行する前に、必ず操作してください。そうでない場合は、動作しません:(この場合、updated_weight = tf.identity(weight))、最初にcontrol_dependenciesで操作を実行します(この場合、opt )

opt = tf.train.Optimizer().minize(loss) with tf.control_dependencies([opt]):#Note the writing format updated_weight = tf.identity(weight) with tf.Session() as sess: tf.global_variables_initializer().run()#This writing format can only be in with because it will find sess by default sess.run(updated_weight, feed_dict={...}) # This way every time you get the updated weight

tf.control_dependencies()にはopが含まれていません:

x = tf.Variable(0.0) x_plus_1 = tf.assign_add(x, 1) # op, means to add 1 to the variable x, here is an op is not returning a tensor with tf.control_dependencies([x_plus_1]): y = x# , op init = tf.global_variables_initializer() with tf.Session() as session: init.run() for i in range(5): print(y.eval())

出力:



0.0 0.0 0.0 0.0 0.0

tf.control_dependencies()にはopが含まれています:

x = tf.Variable(0.0) x_plus_1 = tf.assign_add(x, 1) # op, means to add 1 to the variable x, here is an op is not returning a tensor with tf.control_dependencies([x_plus_1]): y = x op1=tf.group(y) init = tf.global_variables_initializer() with tf.Session() as session: init.run() for i in range(5): session.run(op1) # , so it will execute x_plus_1 to add x to 1 print(y.eval()) # Because x has been added 1 so y will get the value after x plus 1, note: when executing this sentence, x_plus_1 still does not execute

出力:

1.0 2.0 3.0 4.0 5.0

TensorflowはRandomForestを実装します

# -*- encoding:utf-8 -*- import tensorflow as tf from tensorflow.python.ops import resources from tensorflow.contrib.tensor_forest.python import tensor_forest import os from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('../../mnist_data/', one_hot=False) num_steps=500 batch_size=1024 num_features=784 num_trees=10 num_classes = 10 max_nodes = 1000 X=tf.placeholder(tf.float32,shape=[None,784]) Y=tf.placeholder(tf.int32,shape=[None]) # Random Forest Parameters Random Tree Parameters hparams = tensor_forest.ForestHParams(num_classes=num_classes, num_features=num_features, num_trees=num_trees, max_nodes=max_nodes).fill() forest_graph = tensor_forest.RandomForestGraphs(hparams) train_op = forest_graph.training_graph(X,Y) loss_op=forest_graph.training_loss(X,Y) infer_op,_,_ = forest_graph.inference_graph(X) correct_prediction = tf.equal(tf.argmax(infer_op,1),tf.cast(Y,tf.int64)) accuracy_op=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) init_vars=tf.group(tf.global_variables_initializer(),resources.initialize_resources(resources.shared_resources())) sess=tf.Session() sess.run(init_vars) # Training for i in range(1, num_steps + 1): # Prepare Data # Get the next batch of MNIST data (only images are needed, not labels) batch_x, batch_y = mnist.train.next_batch(batch_size) _, l = sess.run([train_op, loss_op], feed_dict={X: batch_x, Y: batch_y}) if i % 50 == 0 or i == 1: acc = sess.run(accuracy_op, feed_dict={X: batch_x, Y: batch_y}) print('Step %i, Loss: %f, Acc: %f' % (i, l, acc)) # Test Model test_x, test_y = mnist.test.images, mnist.test.labels print('Test Accuracy:', sess.run(accuracy_op, feed_dict={X: test_x, Y: test_y}))

出力:

Step 1, Loss: -1.000000, Acc: 0.431641 Step 50, Loss: -254.800003, Acc: 0.870117 Step 100, Loss: -539.599976, Acc: 0.881836 Step 150, Loss: -829.599976, Acc: 0.911133 Step 200, Loss: -1001.000000, Acc: 0.921875 Step 250, Loss: -1001.000000, Acc: 0.922852 Step 300, Loss: -1001.000000, Acc: 0.928711 Step 350, Loss: -1001.000000, Acc: 0.924805 Step 400, Loss: -1001.000000, Acc: 0.911133 Step 450, Loss: -1001.000000, Acc: 0.900391 Step 500, Loss: -1001.000000, Acc: 0.921875 Test Accuracy: 0.9204

参照ブログ:
Tensorflowスタディノート(41):依存関係の制御
ランダムフォレストの実現