1、用命令行进入tensorflow的python编程位置
C:\Users\aDreamer>activate tensorflow
(tensorflow) C:\Users\aDreamer>python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
2、输入代码
>>> import tensorflow as tf #加载tensorflow(导入tensorflow包)
>>> m1=tf.constant([[3,3]]) #定义矩阵常量m1、m2
>>> m2=tf.constant([[2],[3]])
>>> product=tf.matmul(m1,m2) #创建矩阵乘法op,m1、m2相乘
>>> print(product) #打印op
Tensor("MatMul:0", shape=(1, 1), dtype=int32)
>>> sess=tf.Session() #创建会话
2019-05-27 08:41:23.500628: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
>>> result=sess.run(product) #在会话中执行product(执行矩阵乘法op)
>>> print(result) #打印结果
[[15]]
3、几点说明
1、[[3,3]] x [[2],[3]]=3*2+3*3=15
2、关于警告Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
大概意思是:你的CPU支持AVX扩展,但是你安装的TensorFlow版本无法编译使用。
解决:
<1>. 如果是初学者 或者 没有太大计算速度的需求,在开头加上这两行忽略这个提示即可
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
说明:
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1' # 默认,显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3' # 只显示 Error
<2>. 如果需要对CPU进行优化,可以访问下面的github,重新编译tensorflow源码以兼容AVX
https://github.com/lakshayg/tensorflow-build
详情可参考此篇文章:
https://blog.csdn.net/hq86937375/article/details/79696023
这篇博客介绍了如何入门TensorFlow,通过Python代码演示了矩阵乘法操作,并针对出现的'Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2'警告提供了两种解决方案:设置环境变量忽略警告或重新编译TensorFlow源码以兼容AVX。
3万+

被折叠的 条评论
为什么被折叠?



