栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Tensorflow入门

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Tensorflow入门

1、第一个TensorFlow程序

import tensorflow as tf#导入Tensorflow库,并允许我们使用其精彩的功能
#定义一个字符串常量
message = tf.constant("Welcome to the exciting world of Deep Neural Networks!")
with tf.Session() as sess:#启动计算图
    print(sess.run(message).decode())

2、placeholder和feed_dict的使用,placeholder是占位符,在神经网络中常常作为输入数据的节点,而feed_dict可以将数据传进去。写的代码如下,会报错

import tensorflow as tf
y = tf.constant("Hello world")#定义一个常量
x_placeholder = tf.placeholder(tf.string)
with tf.Session() as sess:
    print(sess.run(x_placeholder,feed_dict ={x_placeholder:y}))

报错信息:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("Const:0", shape=(), dtype=string) which was passed to the feed with key Tensor("Placeholder:0", dtype=string).

它说传进去的值不能是Tensor类型。

修改如下:

import tensorflow as tf
#y = tf.constant("Hello world")#定义一个常量
x_placeholder = tf.placeholder(tf.string)
with tf.Session() as sess:
    print(sess.run(x_placeholder,feed_dict ={x_placeholder:"Hello world"}))

这时可以正常输出。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号