tensorflow入门第一讲

tensorflow入门第一讲

NameError: name 'shape' is not defined

解决:

from tensorflow import shape

错误:

TypeError: 'function' object is not subscriptable

解决:
对应函数代码小括号不完整,仔细检查即可
错误:

    w2 = tf.Variable(tf.constant([[0.2,0.1,0.3],[0.2,0.4,0.3]]),name="w2")
    ^
SyntaxError: invalid syntax

解决:
shape后面应该跟等号,而不是小括号

附上全部可正常运行的代码:

import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
from tensorflow import shape
x = tf.constant([0.9,0.85], shape=[1,2])
w1 = tf.Variable(tf.constant([[0.2,0.1,0.3],
[0.2,0.4,0.3]],
shape=[2,3]),name="w1")
w2 = tf.Variable(tf.constant([0.2,0.5,0.25],
shape=[3,1]),name="w2")
b1 = tf.constant([-0.3,0.1,0.2],
shape=[1,3], name="b1")
b2 = tf.constant([-0.3], shape=[1],name="b2")
init_op=tf.global_variables_initializer()
a=tf.matmul(x ,w1)+b1
y=tf.matmul(a ,w2)+b2
with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(y))

错误:

NameError: name 'w2' is not defined

解决:
原因是少写了w2对应的一行代码,补上即可

附正确运行第二段代码:

import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
x = tf.constant([0.9,0.85], shape=[1,2])
w1 = tf.Variable(tf.random_normal([2,3], 
stddev=1, seed=1),name="w1")
w2 = tf.Variable(tf.random_normal([3,1], 
stddev=1, seed=1),name="w1")
b1 = tf.Variable(tf.zeros([1,3]))
b2 = tf.Variable(tf.ones([1]))

init_op=tf.global_variables_initializer()

a = tf.matmul(x ,w1)+b1
y = tf.matmul(a, w2)+b2
with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(y))

以上是前馈神经网络算法练习。

TensorFlow是一个由Google开发的开源机器学习库,它被设计用于构建和部署各种类型的机器学习模型,包括深度学习网络。以下是TensorFlow入门的一些基本步骤: 1. **安装TensorFlow**:首先,你需要下载并安装TensorFlow。如果你是Python开发者,可以通过pip install tensorflow命令来安装最新版本。 2. **环境配置**:推荐使用Anaconda或Jupyter Notebook创建一个独立的虚拟环境,这有助于管理不同项目的依赖。 3. **基础概念**: - **张量(Tensors)**:是TensorFlow的基本数据结构,类似于NumPy中的数组,但支持GPU加速计算。 - **图(Graphs)**:TensorFlow的核心思想是基于图的数据流模型,每个节点代表一个操作,边则表示输入和输出。 4. **创建第一个会话(Session)**:Session是运行图形的地方,你需要用它来执行计算。 5. **代码示例**: - 使用`tf.placeholder`定义占位符,它是动态大小的输入变量。 - 创建常量(`tf.constant`),常数节点不参与图的计算过程。 - 定义运算(`tf.add`, `tf.matmul`等)并将其添加到图中。 - 使用`session.run`执行计算,并获取结果。 6. **实战练习**:尝试解决一些简单的问题,比如线性回归、卷积神经网络的基础应用等,通过实际项目来熟悉API和流程。 7. **官方文档**:查阅TensorFlow官方文档(https://tensorflow.org/)是很关键的,它提供了详细的教程、API参考和案例研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值