tensorflow examples

mnist之于机器学习,便如同hello world之于程序语言。

code

本文主要是代码汇总,不涉及具体理论。

主要内容如下:

1)线性回归

2)logistic回归

3)人工神经网络

4)cnn

5)双端lstm

6)模型的保存和加载

7)其他

  • word2vec

https://github.com/fangpin/daily_programs/blob/master/python/tensorflow/word2vec_basic.py

  • chatbot(seq2seq)

https://github.com/fangpin/daily_programs/tree/master/python/tensorflow/chatbot

  • time series predection(双端lstm)

https://github.com/fangpin/daily_programs/tree/master/python/tensorflow/time-series-predection

免责申明

文中代码难免naive,仅供参考。

线性回归

import numpy as np
import tensorflow as tf

x_data = np.float32(np.random.rand(2,100))
y_data = np.dot([2.0,3.0],x_data) + 0.8

b = tf.Variable(tf.zeros([1]))
w = tf.Variable(tf.random_uniform([1,2],-1.0,1.0))
y = tf.matmul(w,x_data) + b

loss = tf.reduce_mean(tf.square(y-y_data))
train = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

for step in range(1001):
    sess.run(train)
    if step%100 == 0:
        print step , sess.run(w) , sess.run(b)

logistic回归

from __future__ import absolute_import
from __future__ import division

# Import data
import input_data

import tensorflow as tf

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data')

mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

x_data = tf.placeholder(tf.float32,[None,784])

w = tf.Variable(tf.random_uniform([784,10],-1.0,1.0),name='weights')
b = tf.Variable(tf.random_uniform([10],-1.0,1.0),name='biase')
init = tf.initialize_all_variables()

y = tf.nn.softmax(tf.matmul(x_data,w)+b)
y_ = tf.placeholder(tf.float32,[None,10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(1).minimize(cross_entropy)
correct_predection = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
p = tf.reduce_mean(tf.cast(correct_predection,"float"))

tf.scalar_summary('cross_entropy',cross_entro
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值