由浅入深之Tensorflow(2)----logic_regression实现

import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

def initWeights(shape):
    return tf.Variable(tf.random_normal(shape, stddev = 0.1))

def initBiases(shape):
    return tf.Variable(tf.random_normal(shape, stddev = 0.1))

def model(X, weights, baises):
    return tf.matmul(X, weights) + baises

mnist = input_data.read_data_sets(‘MNIST_data/‘, one_hot = True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

X = tf.placeholder(‘float‘, [None, 784])
Y = tf.placeholder(‘float‘, [None, 10])

learning_rate = 0.05
epcoh = 100

weights = initWeights([784,10])
biases = initBiases([10])

y_ = model(X, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_, Y))
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
predict_op = tf.argmax(y_, 1)

with tf.Session() as sess:
    tf.initialize_all_variables().run()
    for i in range(epcoh):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict = {X: trX[start:end], Y: trY[start:end]})
        print (i, np.mean(np.argmax(teY, axis=1) == sess.run(predict_op, feed_dict={X: teX})))
时间: 2024-08-12 04:53:13

由浅入深之Tensorflow(2)----logic_regression实现的相关文章

由浅入深之Tensorflow(1)----linear_regression实现

Tensorflow是目前非常流行的deeplearning框架,学习Tensorflow最好的方法是github上的tf项目https://github.com/tensorflow/tensorflow 或者阅读极客学院主导翻译的中文教程http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html . 此处对tensorflow的基本语法不予赘述,直接贴上源码: import numpy as np i

由浅入深之Tensorflow(4)----Saver&restore

x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) w = tf.Variable(tf.zeros([1, 1], dtype=tf.float32)) b = tf.Variable(tf.ones([1, 1], dtype=tf.float32)) y_hat = tf.add(b, tf.matmul(x, w)) ...more setup for optimization and what not... sav

机器学习进阶笔记之一 | TensorFlow安装与入门

原文链接:https://zhuanlan.zhihu.com/p/22410917 TensorFlow 是 Google 基于 DistBelief 进行研发的第二代人工智能学习系统,被广泛用于语音识别或图像识别等多项机器深度学习领域.其命名来源于本身的运行原理.Tensor(张量)意味着 N 维数组,Flow(流)意味着基于数据流图的计算,TensorFlow 代表着张量从图象的一端流动到另一端计算过程,是将复杂的数据结构传输至人工智能神经网中进行分析和处理的过程. -- 由 UCloud

记一次使用Tensorflow搭建神经网络模型经历

隐去背景, 作者最近第一次用Tensorflow实现训练了几个模型, 其中遇到了一些错误, 把它记录下来 前言 以下提到的所有代码, 都可以在github上面找到. 仓库地址 https://github.com/spxcds/neural_network_code/ 这个仓库里提到的几段代码, 分别实现在从最简单的lr, 到全连接神经网络, 再到卷积神经网络. 从最简单的自己实现交叉熵损失函数, 计算L2正则化, 到后来直接调用库函数, 由简到难, 由浅入深, 截止目前为止, 只实现了MLR,

TensorFlow中的通信机制——Rendezvous(一)本地传输

背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 在TensorFlow源码中我们经常能看到一个奇怪的词--Rendezvous.如果从仔细统计该单词出现的频率和模块,你会发现无论在单机还是分布式,无论在core目录还是contrib目录都存在它的身影,所涉及的模块非常多.Rendezvous是一个法语单词,发音也比较特殊,一般直译为"约会.相会.会和",而在TensorFlow中,Rendezvous是用来完成消

在Win10 Anaconda中安装Tensorflow

有需要的朋友可以参考一下 1.安装Anaconda 下载:https://www.continuum.io/downloads,我用的是Python 3.5 下载完以后,安装. 安装完以后,打开Anaconda Prompt,输入清华的仓库镜像,更新包更快: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --set show_channel_url

Tensorflow 梯度下降实例

# coding: utf-8 # #### 假设我们要最小化函数 $y=x^2$, 选择初始点 $x_0=5$ # #### 1. 学习率为1的时候,x在5和-5之间震荡. # In[1]: import tensorflow as tf TRAINING_STEPS = 10 LEARNING_RATE = 1 x = tf.Variable(tf.constant(5, dtype=tf.float32), name="x") y = tf.square(x) train_op

Ubuntu16.04安装tensorflow+安装opencv+安装openslide+安装搜狗输入法

Ubuntu16.04在cuda以及cudnn安装好之后,安装tensorflow,tensorflow以及opencv可以到网上下载对应的安装包并且直接在安装包所在的路径下直接通过pip与conda进行安装,如下图所示: 前提是要下载好安装包.安装好tensorflow之后还需要进行在~/.bashrc文件中添加系统路径,如下图所示 Openslide是医学图像一个重要的库,这里给出三条命令进行安装 sudo apt-get install openslide-tools sudo apt-g

【tensorflow:Google】三、tensorflow入门

[一]计算图模型 节点是计算,边是数据流, a = tf.constant( [1., 2.] )定义的是节点,节点有属性 a.graph 取得默认计算图 g1 = tf.get_default_graph() 初始化计算图 g1 = tf.Graph() 设置default图 g1.as_default() 定义变量: tf.get_variable('v') 读取变量也是上述函数 对图指定设备 g.device('/gpu:0') 可以定义集合来管理计算图中的资源, 加入集合 tf.add_