本文的目的是介绍TensorflowKeras不适用于参差不齐的张量输入的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于docker–使用TensorFlow后端的Ker
本文的目的是介绍Tensorflow Keras不适用于参差不齐的张量输入的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于docker – 使用TensorFlow后端的Keras不使用GPU、python – TensorFlow中的张量值的条件分配、python,keras,tensorflow 安装问题 module ''tensorflow'' has no attribute ''get_default_graph''、Tensorflow + Keras 深度学习人工智能实践应用 Linux Ubuntu 中 安装Tensroflow 与 Keras的知识。
本文目录一览:- Tensorflow Keras不适用于参差不齐的张量输入
- docker – 使用TensorFlow后端的Keras不使用GPU
- python – TensorFlow中的张量值的条件分配
- python,keras,tensorflow 安装问题 module ''tensorflow'' has no attribute ''get_default_graph''
- Tensorflow + Keras 深度学习人工智能实践应用 Linux Ubuntu 中 安装Tensroflow 与 Keras
Tensorflow Keras不适用于参差不齐的张量输入
我正在使用Tensorflow 2.3。
如果我使用普通的tf张量输入,则以下示例可以正常工作:
import tensorflow as tftext_input = tf.keras.Input([None], dtype=tf.string, name="text_input", ragged=False)predictions = tf.gather(text_input, 0, axis=-1)model = tf.keras.Model(inputs=[text_input], outputs=[predictions])model(tf.constant([[''A1'', ''A2'', ''A3''], [''B1'', ''B2'', ''B3'']]))<tf.Tensor: shape=(2,), dtype=string, numpy=array([b''A1'', b''B1''], dtype=object)>
但是,如果将输入更改为参差不齐的张量,则在尝试创建模型时会出错。
import tensorflow as tfragged_input = tf.keras.Input([None], dtype=tf.string, name="ragged_input", ragged=True)padded_input = ragged_input.to_tensor('''')predictions = tf.gather(padded_input, 0, axis=-1)model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])---------------------------------------------------------------------------InvalidArgumentError Traceback (most recent call last)<ipython-input-201-9adaf4aae2b5> in <module>() 3 padded_input = ragged_input.to_tensor('''') 4 predictions = tf.gather(padded_input, 0, axis=-1)----> 5 model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])13 frames/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 58 ctx.ensure_initialized() 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,---> 60 inputs, attrs, num_outputs) 61 except core._NotOkStatusException as e: 62 if name is not None:InvalidArgumentError: You must feed a value for placeholder tensor ''Placeholder_38'' with dtype int64 and shape [?] [[node Placeholder_38 (defined at <ipython-input-201-9adaf4aae2b5>:5) ]] [Op:__inference_keras_scratch_graph_136790]Function call stack:keras_scratch_graph
答案1
小编典典对我来说似乎是个错误,因为RaggedTensor
对Keras的支持不是最好的(请参见例如此处)。我不确定是什么原因造成的,但是占位符转换失败。
如果可以的话,最好先使用所有RaggedTensor
功能, 然后
再将其作为输入和设置传递ragged=False
。如果您只想使用它进行方便的填充,并且所有图形操作都基于无参差的张量(在您的示例中就是这种情况),那么这不是问题:
import tensorflow as tfragged_input = tf.keras.Input([None], dtype=tf.string, name="ragged_input", ragged=False)# padded_input = ragged_input.to_tensor('''')predictions = tf.gather(ragged_input, 0, axis=-1)model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])padded_input = tf.ragged.constant([[''A1'', ''A2''], [''B1'', ''B2'', ''B3'']]).to_tensor('''')result = model(padded_input)print(result)# >>> tf.Tensor([b''A1'' b''B1''], shape=(2,), dtype=string)
docker – 使用TensorFlow后端的Keras不使用GPU
我用keras版本2.0.0和tensorflow版本0.12.1构建了docker镜像https://github.com/floydhub/dl-docker的gpu版本.然后我运行了mnist教程https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py,但意识到keras没有使用GPU.以下是我的输出
root@b79b8a57fb1f:~/sharedfolder# python test.py
Using TensorFlow backend.
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
x_train shape: (60000,28,1)
60000 train samples
10000 test samples
Train on 60000 samples,validate on 10000 samples
Epoch 1/12
2017-09-06 16:26:54.866833: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions,but these are available on your machine and Could speed up cpu computations.
2017-09-06 16:26:54.866855: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions,but these are available on your machine and Could speed up cpu computations.
2017-09-06 16:26:54.866863: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions,but these are available on your machine and Could speed up cpu computations.
2017-09-06 16:26:54.866870: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions,but these are available on your machine and Could speed up cpu computations.
2017-09-06 16:26:54.866876: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions,but these are available on your machine and Could speed up cpu computations.
如果在keras使用GPU之前需要进行一些设置,有人可以告诉我吗?我对所有这些都很新,所以如果我需要提供更多信息,请告诉我.
我已经安装了page中提到的先决条件
>按照适用于您平台的安装指南安装Docker:https://docs.docker.com/engine/installation/
我能够启动docker镜像
docker run -it -p 8888:8888 -p 6006:6006 -v /sharedfolder:/root/sharedfolder floydhub/dl-docker:cpu bash
> GPU版:无论是来自Nvidia安装在计算机上的Nvidia驱动程序直接或按照指示here.请注意,您不必安装CUDA或cuDNN.这些包含在Docker容器中.
我能够完成最后一步
cv@cv-P15SM:~$cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module 375.66 Mon May 1 15:29:16 PDT 2017
GCC version: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
>仅限GPU版本:按照此处的说明安装nvidia-docker:https://github.com/NVIDIA/nvidia-docker.这将安装docker CLI的替代品.它负责在Docker容器中设置Nvidia主机驱动程序环境以及其他一些东西.
我能够执行步骤here
# Test nvidia-smi
cv@cv-P15SM:~$nvidia-docker run --rm nvidia/cuda nvidia-smi
Thu Sep 7 00:33:06 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.66 Driver Version: 375.66 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 780M Off | 0000:01:00.0 N/A | N/A |
| N/A 55C P0 N/A / N/A | 310MiB / 4036MiB | N/A Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 Not Supported |
+-----------------------------------------------------------------------------+
我也可以运行nvidia-docker命令来启动支持gpu的映像.
我试过了什么
我在下面尝试了以下建议
>检查您是否已完成本教程的第9步(https://github.com/ignaciorlando/skinner/wiki/Keras-and-TensorFlow-installation).注意:您的文件路径在docker镜像中可能完全不同,您必须以某种方式找到它们.
我将建议的行添加到我的bashrc并验证了bashrc文件已更新.
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64:/usr/local/cuda-8.0/extras/CUPTI/lib64' >> ~/.bashrc
echo 'export CUDA_HOME=/usr/local/cuda-8.0' >> ~/.bashrc
>在我的python文件中导入以下命令
进口口
os.environ [“CUDA_DEVICE_ORDER”] =“PCI_BUS_ID”#见问题#152
os.environ [ “CUDA_VISIBLE_DEVICES”] = “0”
不幸的是,这两个步骤单独或一起完成并没有解决问题. Keras仍在以tensorflow的cpu版本作为后端运行.但是,我可能已经找到了可能的问题.我通过以下命令检查了我的tensorflow的版本,并找到了其中两个.
这是cpu版本
root@08b5fff06800:~# pip show tensorflow
Name: tensorflow
Version: 1.3.0
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: opensource@google.com
License: Apache 2.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: tensorflow-tensorboard,six,protobuf,mock,numpy,backports.weakref,wheel
这是GPU版本
root@08b5fff06800:~# pip show tensorflow-gpu
Name: tensorflow-gpu
Version: 0.12.1
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: opensource@google.com
License: Apache 2.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: mock,wheel,six
有趣的是,输出显示keras使用的是tensorflow版本1.3.0,这是cpu版本而不是0.12.1,GPU版本
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.layers import Conv2D,MaxPooling2D
from keras import backend as K
import tensorflow as tf
print('Tensorflow: ',tf.__version__)
产量
root@08b5fff06800:~/sharedfolder# python test.py
Using TensorFlow backend.
Tensorflow: 1.3.0
我想我现在需要弄清楚如何让keras使用tensorflow的gpu版本.
I guess Now I need to figure out how to have keras use the gpu version of tensorflow.
您只需从系统中删除这两个软件包,然后重新安装tensorflow-gpu [评论后更新]:
pip uninstall tensorflow tensorflow-gpu
pip install tensorflow-gpu
此外,令人费解的是你似乎使用了floydhub / dl-docker:cpu容器,而根据说明你应该使用floydhub / dl-docker:gpu one …
python – TensorFlow中的张量值的条件分配
a = np.array([1,2,3,1]) a[a==1] = 0 # a should be [0,0]
如果我在tensorflow中编写类似的代码,我会收到以下错误.
TypeError: 'Tensor' object does not support item assignment
方括号中的条件应该是任意的,如[a <1] = 0. 有没有办法在tensorflow中实现这种“条件赋值”(缺少一个更好的名字)?
解决方法
但是,在直接操作张量时,没有什么能比得上简洁的NumPy语法.您必须使用单独的比较,其中和分配运算符以执行相同的操作.
您的NumPy示例的等效代码是:
import tensorflow as tf a = tf.Variable( [1,1] ) start_op = tf.global_variables_initializer() comparison = tf.equal( a,tf.constant( 1 ) ) conditional_assignment_op = a.assign( tf.where (comparison,tf.zeros_like(a),a) ) with tf.Session() as session: # Equivalent to: a = np.array( [1,1] ) session.run( start_op ) print( a.eval() ) # Equivalent to: a[a==1] = 0 session.run( conditional_assignment_op ) print( a.eval() ) # Output is: # [1 2 3 1] # [0 2 3 0]
print语句当然是可选的,它们只是用于演示代码是否正确执行.
python,keras,tensorflow 安装问题 module ''tensorflow'' has no attribute ''get_default_graph''
module ‘tensorflow’ has no attribute ‘get_default_graph’
当我使用 keras 和 tensorflow 做深度学习的时候,python3.7 报了这个错误,这个问题源自于 keras 和 TensorFlow 的版本过高导致模块不存在或者已经更改不再兼容
解决办法,降级。改为 python3.6.5,TensorFlow1.12.0 和 keras 2.2.4
对应关系如下
python,tensorflow 和 keras 的版本对应关系。
https://docs.floydhub.com/guides/environments/
————————————————
版权声明:本文为 CSDN 博主「devilyouwei」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014466109/article/details/88877321
Anaconda 安装、查看、卸载第三方库
https://blog.csdn.net/qq_35711921/article/details/80495662
Anaconda 详细安装及使用教程(带图文) - 代码帮 - CSDN 博客
https://blog.csdn.net/ITLearnHall/article/details/81708148
Anaconda 版本选择 & Python3.6 版本的 Anaconda 下载
https://blog.csdn.net/Zhou_Dao/article/details/87833957
pip install 安装特别慢
以 tensorflow 为例:
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple/
Tensorflow + Keras 深度学习人工智能实践应用 Linux Ubuntu 中 安装Tensroflow 与 Keras
安装Anaconda
1去Anaconda 官网 下载 64-bit (500多M)
2跳转到下载文件路径下执行安装命令
hadoop@hadoop-GL502VML:~$ cd 下载
hadoop@hadoop-GL502VML:~/下载$ bash Anaconda3-5.1.0-Linux-x86_64.sh -b
Anaconda3-5.1.0-Linux-x86_64.sh 是文件的名字
3加入模块路径
在终端使用
hadoop@hadoop-GL502VML:~$ sudo gedit ~/.bashrc
来跳转到添加路径页面
在计算机上找到安装文件夹中的bin 文件夹 右键属性 得到路径 复制路径
export PATH="/home/hadoop/anaconda3/bin:$PATH"
4使用户设置的环境变量生效
hadoop@hadoop-GL502VML:~$ source ~/.bashrc
5查看python版本
hadoop@hadoop-GL502VML:~$ python --version
安装Tensorflow与Keras
0更新pip
hadoop@hadoop-GL502VML:~$ pip install --upgrade pip
1安装tensorFlow
pip install tensorFlow
2安装Keras
pip install keras
启动Jupyter Notebook
创建工作目录 跳转到工作目录
mkdir -p ~/pywork
cd ~/pywork
在工作目录中输入
jupyter notebook
默认网址是http://localhost:8888
Jupyter Notebook 的使用
1单击new
2单击python3
3单击Untitled可修改NoteBook名称(好了点击ok即可)
Jupyter Notebook 输入命令的方式
今天的关于Tensorflow Keras不适用于参差不齐的张量输入的分享已经结束,谢谢您的关注,如果想了解更多关于docker – 使用TensorFlow后端的Keras不使用GPU、python – TensorFlow中的张量值的条件分配、python,keras,tensorflow 安装问题 module ''tensorflow'' has no attribute ''get_default_graph''、Tensorflow + Keras 深度学习人工智能实践应用 Linux Ubuntu 中 安装Tensroflow 与 Keras的相关知识,请在本站进行查询。
本文标签: