Skip to content

Commit

Permalink
Merge pull request caicloud#4 from Ymx6567/master
Browse files Browse the repository at this point in the history
Update directory structure
  • Loading branch information
perhapszzy authored Feb 14, 2017
2 parents 2d2a9a0 + f3632d4 commit c316ed5
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"# 定义损失函数使得预测少了的损失大,于是模型应该偏向多的方向预测。\n",
"loss_less = 10\n",
"loss_more = 1\n",
"loss = tf.reduce_sum(tf.select(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\n",
"loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\n",
"train_step = tf.train.AdamOptimizer(0.001).minimize(loss)"
]
},
Expand All @@ -73,7 +73,7 @@
"source": [
"rdm = RandomState(1)\n",
"X = rdm.rand(128,2)\n",
"Y = [[x1+x2+rdm.rand()] for (x1, x2) in X]"
"Y = [[x1+x2+rdm.rand()/10.0-0.05] for (x1, x2) in X]"
]
},
{
Expand All @@ -99,24 +99,24 @@
" [ 1.4855988 ]] \n",
"\n",
"After 1000 training step(s), w1 is: \n",
"[[ 0.10801763]\n",
" [ 2.33051562]] \n",
"[[ 0.01247112]\n",
" [ 2.1385448 ]] \n",
"\n",
"After 2000 training step(s), w1 is: \n",
"[[ 0.69744456]\n",
" [ 2.66114545]] \n",
"[[ 0.45567414]\n",
" [ 2.17060661]] \n",
"\n",
"After 3000 training step(s), w1 is: \n",
"[[ 1.01532638]\n",
" [ 2.6881988 ]] \n",
"[[ 0.69968724]\n",
" [ 1.8465308 ]] \n",
"\n",
"After 4000 training step(s), w1 is: \n",
"[[ 1.29930842]\n",
" [ 2.56087017]] \n",
"[[ 0.89886665]\n",
" [ 1.29736018]] \n",
"\n",
"Final w1 is: \n",
"[[ 1.50898325]\n",
" [ 2.31391478]]\n"
"[[ 1.01934695]\n",
" [ 1.04280889]]\n"
]
}
],
Expand Down Expand Up @@ -154,35 +154,97 @@
"output_type": "stream",
"text": [
"After 0 training step(s), w1 is: \n",
"[[-0.81031823]\n",
" [ 1.4855988 ]] \n",
"[[-0.81231821]\n",
" [ 1.48359871]] \n",
"\n",
"After 1000 training step(s), w1 is: \n",
"[[ 0.06754676]\n",
" [ 1.68602526]] \n",
"[[ 0.18643527]\n",
" [ 1.07393336]] \n",
"\n",
"After 2000 training step(s), w1 is: \n",
"[[ 0.62151301]\n",
" [ 1.49415982]] \n",
"[[ 0.95444274]\n",
" [ 0.98088616]] \n",
"\n",
"After 3000 training step(s), w1 is: \n",
"[[ 0.85797542]\n",
" [ 1.32460475]] \n",
"[[ 0.95574027]\n",
" [ 0.9806633 ]] \n",
"\n",
"After 4000 training step(s), w1 is: \n",
"[[ 1.01017809]\n",
" [ 1.2008121 ]] \n",
"[[ 0.95466018]\n",
" [ 0.98135227]] \n",
"\n",
"Final w1 is: \n",
"[[ 1.08652937]\n",
" [ 1.13757098]]\n"
"[[ 0.95525807]\n",
" [ 0.9813394 ]]\n"
]
}
],
"source": [
"loss_less = 1\n",
"loss_more = 10\n",
"loss = tf.reduce_sum(tf.select(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\n",
"loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\n",
"train_step = tf.train.AdamOptimizer(0.001).minimize(loss)\n",
"\n",
"with tf.Session() as sess:\n",
" init_op = tf.global_variables_initializer()\n",
" sess.run(init_op)\n",
" STEPS = 5000\n",
" for i in range(STEPS):\n",
" start = (i*batch_size) % 128\n",
" end = (i*batch_size) % 128 + batch_size\n",
" sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})\n",
" if i % 1000 == 0:\n",
" print(\"After %d training step(s), w1 is: \" % (i))\n",
" print sess.run(w1), \"\\n\"\n",
" print \"Final w1 is: \\n\", sess.run(w1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 6. 定义损失函数为MSE。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"After 0 training step(s), w1 is: \n",
"[[-0.81031823]\n",
" [ 1.4855988 ]] \n",
"\n",
"After 1000 training step(s), w1 is: \n",
"[[-0.13337609]\n",
" [ 1.81309223]] \n",
"\n",
"After 2000 training step(s), w1 is: \n",
"[[ 0.32190299]\n",
" [ 1.52463484]] \n",
"\n",
"After 3000 training step(s), w1 is: \n",
"[[ 0.67850214]\n",
" [ 1.25297272]] \n",
"\n",
"After 4000 training step(s), w1 is: \n",
"[[ 0.89473999]\n",
" [ 1.08598232]] \n",
"\n",
"Final w1 is: \n",
"[[ 0.97437561]\n",
" [ 1.0243336 ]]\n"
]
}
],
"source": [
"loss = tf.losses.mean_squared_error(y, y_)\n",
"train_step = tf.train.AdamOptimizer(0.001).minimize(loss)\n",
"\n",
"with tf.Session() as sess:\n",
Expand Down Expand Up @@ -222,4 +284,3 @@
"nbformat": 4,
"nbformat_minor": 1
}

28 changes: 14 additions & 14 deletions Deep_Learning_with_TensorFlow/0.12.0/Chapter10/2. 多GPU并行.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
N_GPU = 4

# 定义日志和模型输出的路径。
MODEL_SAVE_PATH = "/path/to/logs_and_models/"
MODEL_SAVE_PATH = "logs_and_models/"
MODEL_NAME = "model.ckpt"
DATA_PATH = "/path/to/data.tfrecords"
DATA_PATH = "output.tfrecords"

# 定义输入队列得到训练数据,具体细节可以参考第七章。
def get_input():
filename_queue = tf.train.string_input_producer([DATA_PATH])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
_, serialized_example = reader.read(filename_queue)

# 定义数据解析格式。
features = tf.parse_single_example(
Expand All @@ -44,17 +44,17 @@ def get_input():
# 定义输入队列并返回。
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * BATCH_SIZE
return tf.train.shuffle_batch(
return tf.train.shuffle_batch(
[retyped_image, label],
batch_size=BATCH_SIZE,
capacity=capacity,
min_after_dequeue=min_after_dequeue)

# 定义损失函数。
def get_loss(x, y_, regularizer, scope):
y = mnist_inference.inference(x, regularizer)
cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
regularization_loss = tf.add_n(tf.get_collection('losses', scope))
y = mnist_inference.inference(x, regularizer)
cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
regularization_loss = tf.add_n(tf.get_collection('losses', scope))
loss = cross_entropy + regularization_loss
return loss

Expand Down Expand Up @@ -82,7 +82,7 @@ def average_gradients(tower_grads):
# 主训练过程。
def main(argv=None):
# 将简单的运算放在CPU上,只有神经网络的训练过程放在GPU上。
with tf.Graph().as_default(), tf.device('/cpu:0'):
with tf.Graph().as_default(), tf.device('/cpu:0'):

# 定义基本的训练过程
x, y_ = get_input()
Expand Down Expand Up @@ -110,29 +110,29 @@ def main(argv=None):
grads = average_gradients(tower_grads)
for grad, var in grads:
if grad is not None:
tf.histogram_summary('gradients_on_average/%s' % var.op.name, grad)
tf.summary.histogram('gradients_on_average/%s' % var.op.name, grad)

# 使用平均梯度更新参数。
apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
for var in tf.trainable_variables():
tf.histogram_summary(var.op.name, var)
tf.summary.histogram(var.op.name, var)

# 计算变量的滑动平均值。
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
# 每一轮迭代需要更新变量的取值并更新变量的滑动平均值。
train_op = tf.group(apply_gradient_op, variables_averages_op)

saver = tf.train.Saver(tf.all_variables())
summary_op = tf.merge_all_summaries()
init = tf.initialize_all_variables()
saver = tf.train.Saver(tf.global_variables())
summary_op = tf.summary.merge_all()
init = tf.global_variables_initializer()
with tf.Session(config=tf.ConfigProto(
allow_soft_placement=True, log_device_placement=True)) as sess:
# 初始化所有变量并启动队列。
init.run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
summary_writer = tf.train.SummaryWriter(MODEL_SAVE_PATH, sess.graph)
summary_writer = tf.summary.FileWriter(MODEL_SAVE_PATH, sess.graph)

for step in range(TRAINING_STEPS):
# 执行神经网络训练操作,并记录训练操作的运行时间。
Expand Down
Loading

0 comments on commit c316ed5

Please sign in to comment.