假设我有一个Tensorflow张量.如何将张量的尺寸(形状)作为整数值?我知道有两种方法,tensor.get_shape()以及tf.shape(tensor),但我不能让形状值作为整int32数值.
例如,下面我创建了一个二维张量,我需要得到行数和列数,int32以便我可以调用reshape()以创建一个形状的张量(num_rows * num_cols, 1).但是,该方法tensor.get_shape()返回值作为Dimension类型,而不是int32.
import tensorflow as tf
import numpy as np
sess = tf.Session()
tensor = tf.convert_to_tensor(np.array([[1001,1002,1003],[3,4,5]]), dtype=tf.float32)
sess.run(tensor)
# array([[ 1001., 1002., 1003.],
# [ 3., 4., 5.]], dtype=float32)
tensor_shape = tensor.get_shape()
tensor_shape
# TensorShape([Dimension(2), Dimension(3)])
print tensor_shape
# (2, 3)
num_rows = tensor_shape[0] # ???
num_cols = tensor_shape[1] # ???
tensor2 = tf.reshape(tensor, (num_rows*num_cols, 1))
# Traceback (most …Run Code Online (Sandbox Code Playgroud) 当我尝试在 Apple M1 上安装 TensorFlow 时,遇到 4 个问题:
Conda 自 2022 年 5 月 6 日起就支持 M1,但我在 google 上搜索的大多数文章都讨论了使用 Miniforge,例如,所以我觉得它们都有点过时了。
我使用最新的 conda 4.13 成功设置了我的 python 环境(3.8、3.9 和 3.10),但是当我尝试安装 tensorflow 时,出现错误“找不到tensorflow 的匹配发行版”(全部失败)。
ERROR: Could not find a version that satisfies the requirement tensorflow (from …Run Code Online (Sandbox Code Playgroud)我知道我可以测量一次调用的执行时间sess.run(),但是有可能获得更精细的粒度并测量单个操作的执行时间吗?
我正在研究TensorFlow以及如何使用它,即使我不是神经网络和深度学习(只是基础知识)的专家.
以下教程中,我不理解三个优化器之间的实际差异.我看看API,我理解原则,但我的问题是:
1.何时优先使用一个而不是其他一个?
2.知道有重要的区别吗?
在哪些情况下应该考虑tf.Session()和tf.InteractiveSession()考虑用于什么目的?
当我尝试使用前者时,某些功能(例如.eval())不起作用,当我改为后者时,它起作用了.
我已经使用了tensorflow一天,但是有一些麻烦,当我导入tensorflow时,会出现AttributeError:'module'对象没有属性'XXXXXX'
我使用ubuntu14.04,python2.7,CUDA工具包8.0和CuDNN v5.我的六个和protobuf的版本是:名称:六个版本:1.10.0位置:/usr/local/lib/python2.7/dist-packages需要:名称:protobuf版本:3.2.0位置:/ usr/local/lib/python2.7/dist-packages需要:six,setuptools
这是我的测试代码:
import tensorflow as tf
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
mul = tf.mul(a, b)
with tf.Session() as sess:
# Run every operation with variable input
print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
tensorflow安装有问题吗?还是其他任何问题?
根据TensorFlow 文档,类prefetch和map方法tf.contrib.data.Dataset,都有一个名为的参数buffer_size.
对于prefetch方法,该参数称为buffer_size并且根据文档:
buffer_size:tf.int64标量tf.Tensor,表示预取时将被缓冲的最大元素数.
对于该map方法,该参数称为output_buffer_size并且根据文档:
output_buffer_size :(可选.)tf.int64标量tf.Tensor,表示将被缓冲的最大处理元素数.
类似地,对于该shuffle方法,出现相同的数量并且根据文档:
buffer_size:tf.int64标量tf.Tensor,表示新数据集将从中采样的数据集中的元素数.
这些参数之间有什么关系?
假设我创建一个Dataset对象如下:
tr_data = TFRecordDataset(trainfilenames)
tr_data = tr_data.map(providefortraining, output_buffer_size=10 * trainbatchsize, num_parallel_calls\
=5)
tr_data = tr_data.shuffle(buffer_size= 100 * trainbatchsize)
tr_data = tr_data.prefetch(buffer_size = 10 * trainbatchsize)
tr_data = tr_data.batch(trainbatchsize)
Run Code Online (Sandbox Code Playgroud)
buffer上述代码段中的参数扮演了什么角色?
当我跑步时,sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})我得到了InternalError: Blas SGEMM launch failed.这是完整的错误和堆栈跟踪:
InternalErrorTraceback (most recent call last)
<ipython-input-9-a3261a02bdce> in <module>()
1 batch_xs, batch_ys = mnist.train.next_batch(100)
----> 2 sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
338 try:
339 result = self._run(None, fetches, feed_dict, options_ptr,
--> 340 run_metadata_ptr)
341 if run_metadata:
342 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
562 try:
563 results = self._do_run(handle, target_list, unique_fetches,
--> 564 feed_dict_string, options, run_metadata) …Run Code Online (Sandbox Code Playgroud) 我刚刚在ubuntu上重新安装了最新的tensorflow:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl
[sudo] password for ubuntu:
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If …Run Code Online (Sandbox Code Playgroud) 官方Tensorflow API文档声称对于tf.layers.conv2d和tf.layers.dense ,参数kernel_initializer默认为None.
但是,阅读图层教程(https://www.tensorflow.org/tutorials/layers),我注意到该参数未在代码中设置.例如:
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
Run Code Online (Sandbox Code Playgroud)
本教程中的示例代码运行时没有任何错误,因此我认为默认kernel_initializer值不是None.那么,使用哪个初始化程序?
在另一个代码中,我没有设置tf.layers.conv2dconv2d和密集层,一切都很好.然而,当我试图设置tf.layers.dense到kernel_initializer,我得到NaN的错误.这里发生了什么?有人可以帮忙吗?