一直在等待,一直会等待 TensorFlow常见API--1

tf.placeholder(dtype, shape=None,name=None)

         张量的占位符号,执行的时候一定要通过数据进行赋值,否则会出错。
注意:
Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict optional argument to Session.run(), Tensor.eval(), or Operation.run()
dtype: 元素类型
shape: 张量的形状,默认情况下为任意形状
name: 该操作名称
返回值:将用于数据填充的句柄

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  print(sess.run(y))  # ERROR: will fail because x was not fed.

  rand_array = np.random.rand(1024, 1024)
  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

tf.reshape(tensor,shape,name=None)

        返回一个给定张量的按照给定的形状的新张量
tensor: 给定张量
shape: 给定张量的形状
name: 该操作名称

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
                        [4, 5, 6],
                        [7, 8, 9]]

# tensor 't' is [[[1, 1], [2, 2]],
#                [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
                        [3, 3, 4, 4]]

# tensor 't' is [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]

# -1 can also be used to infer the shape

# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         [4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         [4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
                              [2, 2, 2],
                              [3, 3, 3]],
                             [[4, 4, 4],
                              [5, 5, 5],
                              [6, 6, 6]]]

# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7

tf.argmax(input,axis=None,name=None,dimension=None,output_type=tf.int64

)
        返回给定坐标轴方向下最大值的索引
input: 给定张量
axis: 给定坐标轴,0表示按列,1表示按行
name: 该操作名称
dimension:和axis功能一样,默认axis取值优先
output_type:输出数据的类型,tf.int32, tf.int64

import tensorflow as tf

a=tf.get_variable(name='a',
                  shape=[3,4],
                  dtype=tf.float32,
                  initializer=tf.random_uniform_initializer(minval=-1,maxval=1))
b=tf.argmax(input=a,axis=0)
c=tf.argmax(input=a,dimension=1)   #此处用dimesion或用axis是一样的
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
print(sess.run(a))
#[[ 0.04261756 -0.34297419 -0.87816691 -0.15430689]
# [ 0.18663144  0.86972666 -0.06103253  0.38307118]
# [ 0.84588599 -0.45432305 -0.39736366  0.38526249]]
print(sess.run(b))
#[2 1 1 2]
print(sess.run(c))
#[0 1 0]

        代码用例来自:https://blog.youkuaiyun.com/liyaoqing/article/details/54020202

tf.get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=None)

Gets an existing variable with these parameters or create a new one
        给定参数获取一个存在的变量或者创建一个新的变量
name: 该操作名称
shape: 给定张量的形状
dtype:张量的数据类型
initializer: 张量的初始化器。要么为初始化对象,要么为张量

def foo():
  with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
    v = tf.get_variable("v", [1])
  return v

v1 = foo()  # Creates v.
v2 = foo()  # Gets the same, existing v.
assert v1 == v2
### 解决 Python 中 `ModuleNotFoundError: No module named 'tensorflow'` 的方法 当在 Python 环境中尝试导入 TensorFlow 模块时,如果出现 `ModuleNotFoundError: No module named 'tensorflow'` 错误,则表明当前环境中尚未正确安装该模块。以下是针对此问题的全面解析和解决方案。 #### 1. **确认错误原因** 该错误的主要原因是目标模块(即 TensorFlow)未被正确安装到所使用的 Python 环境中。这可能是由于以下原因之一造成的: - 当前环境缺少 TensorFlow 安装。 - 使用了多个 Python 环境,而 TensorFlow 被安装到了其他环境中。 - 版本冲突或依赖项缺失导致无法正常加载模块[^1]。 #### 2. **验证当前环境中的 TensorFlow 是否存在** 可以通过命令行工具来检查 TensorFlow 是否已经安装。打开终端并输入以下命令: ```bash pip show tensorflow ``` 如果没有返回任何信息,则说明 TensorFlow 尚未安装;如果有输出,则可以进一步查看其版本号和其他细节以确保兼容性[^2]。 #### 3. **重新安装 TensorFlow** 如果发现 TensorFlow 缺失或者版本不符合需求,可通过 pip 或 conda 来重新安装它。具体操作如下: ##### (a) 使用 Pip 进行安装 对于大多数标准情况,推荐使用官方支持的方式通过 pip 安装最新稳定版 TensorFlow: ```bash pip install --upgrade tensorflow ``` 注意:如果你正在使用的是 Python 3.x,请确保调用了对应版本的 pip 命令(如 `pip3`)。此外,在某些操作系统上可能还需要管理员权限,此时可加上 `-m` 参数执行脚本模式下的升级过程[^4]。 ##### (b) 利用 Conda 创建独立虚拟环境后再安装 为了防止污染全局 python 配置文件夹以及更好地管理不同项目的依赖关系,建议创建一个新的 conda 虚拟环境专门用于处理涉及 deep learning 库的任务: ```bash conda create -n tf_env python=3.8 conda activate tf_env conda install tensorflow ``` 这里假设选择了 Python 3.8作为基础解释器版本——当然也可以根据实际需要调整至合适的选项范围内[^3]。 #### 4. **排查常见陷阱** 即使完成了上述步骤之后仍然遇到了同样的 ImportError 提示符的话,那么很可能还存在着一些隐藏的问题等待解决。下面列举了几种常见的可能性及其对应的修复策略: - **多版本共存引发混乱**: 如果系统里同时存在多种不同的 Pythons 实现形式(比如 Anaconda 和 Miniconda),则有可能因为默认优先级设置不当而导致最终激活出来的并非预期的那个 runtime instance 。因此务必明确指定完整路径名来进行后续的所有交互动作 ,例如 `/home/user/miniconda3/bin/python myscript.py`. - **IDE 设置异常**: 对于那些习惯借助集成开发平台完成日常编码工作的开发者而言, 往往会忽略掉关于 project interpreter configuration 方面的知识点 . 所以记得核查一下目前 active state 下关联的具体 location 是不是指向刚才新建好的 isolated sandbox area inside which we've just successfully incorporated the required package into earlier . - **硬件加速驱动不足**: 若打算充分利用 GPU 加速计算性能提升训练效率 , 则除了单纯引入 base-level API 外部接口之外 , 另外还得额外关注 CUDA Toolkit & cuDNN Library 的配套部署状况 . --- ### 总结 综上所述,要彻底消除 `ModuleNotFoundError: No module named 'tensorflow'` 的困扰,需依次经历以下几个阶段的工作流程:先诊断清楚根本诱因所在位置 -> 接着采取针对性措施予以纠正 -> 最终再辅以必要的辅助检验手段加以巩固成果^[]^. ```python import tensorflow as tf print(tf.__version__) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值