現在以下のサイトをみて、勉強しています。
http://www.buildinsider.net/small/booktensorflow/0203
_loss ・_train 関数を書く場所はどこなのでしょうか?
サイトでは
def _loss(logits, label): labels = tf.cast(label, tf.int64) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits, labels, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') return cross_entropy_mean def _train(total_loss, global_step): opt = tf.train.GradientDescentOptimizer(learning_rate=0.001) grads = opt.compute_gradients(total_loss) train_op = opt.apply_gradients(grads, global_step=global_step) return train_op filenames = [ os.path.join( FLAGS.data_dir,'data_batch_%d.bin' % i) for i in range(1, 6) ] def main(argv=None): global_step = tf.Variable(0, trainable=False) train_placeholder = tf.placeholder(tf.float32, shape=[32, 32, 3], name='input_image') label_placeholder = tf.placeholder(tf.int32, shape=[1], name='label') ・ ・ ・ ・ ・
のように書いていて、私の場合だと、inference.pyのfilenames ・def main(argv=None): の上に書くのかなと思いました。
自分のinference.py
# coding: UTF-8 from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import time import tensorflow as tf import model as model import numpy as np from reader import Cifar10Reader FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_integer('epoch', 30, "訓練するEpoch数") tf.app.flags.DEFINE_string('data_dir', './data/', "訓練データのディレクトリ") tf.app.flags.DEFINE_string('checkpoint_dir', './checkpoints/', "チェックポイントを保存するディレクトリ") filenames = [ os.path.join( FLAGS.data_dir, 'data_batch_%d.bin' % i) for i in range(1, 6) ] def main(argv=None): global_step = tf.Variable(0,trainable=False) train_placeholder = tf.placeholder(tf.float32, shape=[32, 32, 3], name='input_image') label_placeholder = tf.placeholder(tf.int32,shape=[1],name='label') # (width, height, depth) -> (batch, width, height, depth) image_node = tf.expand_dims(train_placeholder, 0) logits = model.inference(image_node) total_loss = _loss(logits,label_placeholder) train_op = _train(total_loss,global_step) top_k_op = tf.nn.in_top_k(logits,label_placeholder,1) with tf.Session() as sess: ・ ・ ・ ・ ・
しかし、inference.pyのmainメソッドで推論など、コアな処理を行っているのでmainメソッドの下に書くべきなのではないかなと思っています。
やはり、mainメソッドの上に_loss ・_train 関数を書くべきなのでしょうか?
ちなみに、_loss ・_train 関数の名前の前についている_はどのような意味なのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。