楼主: ReneeBK
1520 19

[Jupyter] Learning TensorFlow [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


本帖隐藏的内容

https://github.com/Hezi-Resheff/Oreilly-Learning-TensorFlow



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Learning earning Tensor Learn flow

本帖被以下文库推荐

沙发
ReneeBK 发表于 2017-8-13 06:48:32 |只看作者 |坛友微信交流群
  1. from tensorflow.examples.tutorials.mnist import input_data
  2. import tensorflow as tf
  3. import numpy as np

  4. DATA_DIR = '/tmp/data'
  5. MINIBATCH_SIZE = 50
  6. STEPS = 5000

  7. from layers import *

  8. mnist = input_data.read_data_sets(DATA_DIR, one_hot=True)

  9. x = tf.placeholder(tf.float32, shape=[None, 784])
  10. y_ = tf.placeholder(tf.float32, shape=[None, 10])

  11. x_image = tf.reshape(x, [-1, 28, 28, 1])
  12. conv1 = conv_layer(x_image, shape=[5, 5, 1, 32])
  13. conv1_pool = max_pool_2x2(conv1)

  14. conv2 = conv_layer(conv1_pool, shape=[5, 5, 32, 64])
  15. conv2_pool = max_pool_2x2(conv2)

  16. conv2_flat = tf.reshape(conv2_pool, [-1, 7*7*64])
  17. full_1 = tf.nn.relu(full_layer(conv2_flat, 1024))

  18. keep_prob = tf.placeholder(tf.float32)
  19. full1_drop = tf.nn.dropout(full_1, keep_prob=keep_prob)

  20. y_conv = full_layer(full1_drop, 10)

  21. cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y_))
  22. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  23. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  24. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  25. with tf.Session() as sess:
  26.     sess.run(tf.global_variables_initializer())

  27.     for i in range(STEPS):
  28.         batch = mnist.train.next_batch(MINIBATCH_SIZE)

  29.         if i % 100 == 0:
  30.             train_accuracy = sess.run(accuracy, feed_dict={x: batch[0], y_: batch[1],
  31.                                                            keep_prob: 1.0})
  32.             print("step {}, training accuracy {}".format(i, train_accuracy))

  33.         sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

  34.     X = mnist.test.images.reshape(10, 1000, 784)
  35.     Y = mnist.test.labels.reshape(10, 1000, 10)
  36.     test_accuracy = np.mean([sess.run(accuracy, feed_dict={x:X[i], y_:Y[i], keep_prob:1.0}) for i in range(10)])

  37. print("test accuracy: {}".format(test_accuracy))
复制代码

使用道具

藤椅
ReneeBK 发表于 2017-8-13 06:48:55 |只看作者 |坛友微信交流群
  1. import tensorflow as tf


  2. def weight_variable(shape):
  3.     initial = tf.truncated_normal(shape, stddev=0.1)
  4.     return tf.Variable(initial)


  5. def bias_variable(shape):
  6.     initial = tf.constant(0.1, shape=shape)
  7.     return tf.Variable(initial)


  8. def conv2d(x, W):
  9.     return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


  10. def max_pool_2x2(x):
  11.     return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  12.                           strides=[1, 2, 2, 1], padding='SAME')


  13. def conv_layer(input, shape):
  14.     W = weight_variable(shape)
  15.     b = bias_variable([shape[3]])
  16.     return tf.nn.relu(conv2d(input, W) + b)


  17. def full_layer(input, size):
  18.     in_size = int(input.get_shape()[1])
  19.     W = weight_variable([in_size, size])
  20.     b = bias_variable([size])
  21.     return tf.matmul(input, W) + b
复制代码

使用道具

板凳
ReneeBK 发表于 2017-8-13 06:50:13 |只看作者 |坛友微信交流群
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Dec 20 17:34:43 2016

  4. @author: tomhope
  5. """
  6. from __future__ import print_function
  7. import tensorflow as tf
  8. from tensorflow.examples.tutorials.mnist import input_data
  9. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

  10. element_size = 28;time_steps = 28;num_classes = 10
  11. batch_size = 128;hidden_layer_size = 128

  12. _inputs = tf.placeholder(tf.float32,shape=[None, time_steps,
  13.                                            element_size],
  14.                                            name='inputs')
  15. y = tf.placeholder(tf.float32, shape=[None, num_classes],name='inputs')

  16. #TensorFlow built-in functions
  17. rnn_cell = tf.contrib.rnn.BasicRNNCell(hidden_layer_size)
  18. outputs, _ = tf.nn.dynamic_rnn(rnn_cell, _inputs, dtype=tf.float32)

  19. Wl = tf.Variable(tf.truncated_normal([hidden_layer_size, num_classes],
  20.                                      mean=0,stddev=.01))
  21. bl = tf.Variable(tf.truncated_normal([num_classes],mean=0,stddev=.01))

  22. def get_linear_layer(vector):
  23.     return tf.matmul(vector, Wl) + bl

  24. last_rnn_output = outputs[:,-1,:]
  25. final_output = get_linear_layer(last_rnn_output)

  26. softmax = tf.nn.softmax_cross_entropy_with_logits(logits=final_output, labels=y)
  27. cross_entropy = tf.reduce_mean(softmax)
  28. train_step = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cross_entropy)

  29. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(final_output,1))
  30. accuracy = (tf.reduce_mean(tf.cast(correct_prediction, tf.float32)))*100

  31. sess=tf.InteractiveSession()
  32. sess.run(tf.global_variables_initializer())

  33. test_data = mnist.test.images[:batch_size].reshape((-1,
  34.                                             time_steps, element_size))
  35. test_label = mnist.test.labels[:batch_size]
  36.   
  37. for i in range(3001):

  38.        batch_x, batch_y = mnist.train.next_batch(batch_size)
  39.        batch_x = batch_x.reshape((batch_size, time_steps, element_size))
  40.        sess.run(train_step,feed_dict={_inputs:batch_x,
  41.                                       y:batch_y})
  42.        if i % 1000 == 0:
  43.             acc = sess.run(accuracy, feed_dict={_inputs: batch_x,
  44.                                                 y: batch_y})
  45.             loss = sess.run(cross_entropy,feed_dict={_inputs:batch_x,
  46.                                                      y:batch_y})
  47.             print ("Iter " + str(i) + ", Minibatch Loss= " + \
  48.                   "{:.6f}".format(loss) + ", Training Accuracy= " + \
  49.                   "{:.5f}".format(acc))   

  50. print ("Testing Accuracy:", \
  51.     sess.run(accuracy, feed_dict={_inputs: test_data, y: test_label}))
  52.    
复制代码

使用道具

报纸
hjtoh 发表于 2017-8-13 06:57:01 来自手机 |只看作者 |坛友微信交流群
ReneeBK 发表于 2017-8-13 06:48
**** 本内容被作者隐藏 ****
写字的分享

使用道具

地板
MouJack007 发表于 2017-8-13 07:35:58 |只看作者 |坛友微信交流群
谢谢楼主分享!

使用道具

7
MouJack007 发表于 2017-8-13 07:36:18 |只看作者 |坛友微信交流群

使用道具

8
军旗飞扬 发表于 2017-8-13 07:45:52 |只看作者 |坛友微信交流群
谢谢楼主分享!

使用道具

9
yazxf 发表于 2017-8-13 08:21:23 |只看作者 |坛友微信交流群
谢谢你的书!

使用道具

10
lm972 发表于 2017-8-13 08:37:39 |只看作者 |坛友微信交流群
谢谢分享

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 14:32