Tensorflow 学习笔记- tensor的声明

  1. tf 声明Variable变量时,在使用前需要对变量进行初始化,才可运行tensor
import tensorflow as tf
// case one
a = tf.Variable(4, tf.int16)
b = tf.Variable([[4,5], [3,5]])
c = tf.add(a,b)
// init the variable
init =tf.global_variables_initializer()
with tf.Session() as sess: // the session will close automatically
    sess.run(init) // must do it firstly
    sess.run(c)

// case two
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print sess.run(state)
    for _ in range(3):
        sess.run(update)
        print sess.run(state)

代码中assign() 操作是图所描绘的表达式的一部分, 正如add() 操作一样. 所以在调用run() 执行表达式
之前, 它并不会真正执行赋值操作.

2.Fetch
为了取回操作的输出内容, 可以在使用Session 对象的run() 调用 执行图时, 传入一些 tensor, 这些 tens
or 会帮助你取回结果.

input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session() as sess:
    result = sess.run([mul, intermed])
    print result
# 输出:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

需要获取的多个 tensor 值,在 op 的一次运行中一起获得(而不是逐个去取 tensor)

3.Feed
Tensorflow 提供feed机制,该机制可以临时替代graph中任意操作中的tensor,直接插入一个tensor。可以提供feed数据作为run()调用的参数。因此,feed只有在调用它的method内有效。最常见的例子事将某些特殊的操作指定为“feed”操作,标记的方法是使用tf.placeholder()为这些操作创建占位符。

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.add(input1, input2)

with tf.Session() as sess:
    print sess.run([output], feed_dict={input1:[7], input2:[3]})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: TensorFlow与scikit-learn的接口是不同的。scikit-learn的设计原则是所有对象的接口一致且简单,它使用估计器(estimator)和转换器(transformer)的概念。估计器是基于数据集的对象,可以对一些参数进行估计,比如RandomForest()、LinearRegression()。转换器是可以转换数据集中数值的估计器,比如处理缺失值的SimpleImputer()。转换器的API调用过程是通过transform()方法进行转换,将被转换的数据集作为参数,返回经过转换的数据集。\[3\] 而TensorFlow的接口与scikit-learn有所不同。TensorFlow是一个用于构建和训练神经网络的深度学习框架,它提供了更底层的操作和更灵活的接口。在TensorFlow中,你需要自己定义神经网络的结构和训练过程,使用张量(Tensor)和计算图(Graph)进行操作。你可以使用TensorFlow的高级API,如Keras,来简化神经网络的构建和训练过程。但与scikit-learn不同,TensorFlow没有明确的估计器和转换器的概念。\[1\] 因此,如果你已经掌握了算法理论的各个方面,你可以使用scikit-learn来实现你设计的算法,而临时查一下文档通常就足够了。而对于TensorFlow,你需要更深入地了解其底层操作和API,以便构建和训练神经网络。\[1\] #### 引用[.reference_title] - *1* [scikit-learn, tensorflow, pytorch真的只需要查下API,不需要学吗?](https://blog.csdn.net/woshicver/article/details/107650395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Scikit-Learn | 自定义转换器(transformer)](https://blog.csdn.net/SanyHo/article/details/105409961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值