import tensorflow as tf
import numpy as np
tf.reset_default_graph()
x = [[1, 0, 0],
[2, 3, 0],
[4, 5, 6],
[7, 8, 0],
[9, 0, 0],
[0, 1, 0]]
x_new = [np.array(i) for i in x]
#tf.TensorShape([]) 表示长度为单个数字
#tf.TensorShape([None]) 表示长度未知的向量
padded_shapes=(
tf.TensorShape([None])
)
# padded_shapes=(
# tf.TensorShape([None]),
# )
#TypeError: Expected int64, got TensorShape([Dimension(None)]) of type 'TensorShape' instead.
# 注意,在tf.TensorShape([None])后面不能添加 ",",因为这里递归嵌套,会认为","后面还有一维数据,
# 只是数据格式为 None。
dataset = tf.data.Dataset.from_tensor_slices(x)
dataset = dataset.padded_batch(2, padded_shapes=padded_shapes)
iterator = dataset.make_one_shot_iterator()
sess = tf.Session()
try:
while True:
print(sess.run(iterator.get_next()))
except tf.errors.OutOfRangeError:
print("end")
dataset = tf.data.Dataset.range(100)
dataset = dataset.map(lambda x: tf.fill([tf.cast(x, tf.int32)], x))
dataset = dataset.padded_batch(4, padded_shapes=[None])
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
print(sess.run(next_element)) # ==> [[0, 0, 0], [1, 0, 0], [2, 2, 0], [3, 3, 3]]
print(sess.run(next_element)) # ==> [[4, 4, 4, 4, 0, 0, 0],
print(sess.run(next_element)) # [5, 5, 5, 5, 5, 0, 0],
print(sess.run(next_element)) # [6, 6, 6, 6, 6, 6, 0],
print(sess.run(next_element)) # [7, 7, 7, 7, 7, 7, 7]]
[[1 0 0]
[2 3 0]]
[[4 5 6]
[7 8 0]]
[[9 0 0]
[0 1 0]]
end
[[0 0 0]
[1 0 0]
[2 2 0]
[3 3 3]]
[[4 4 4 4 0 0 0]
[5 5 5 5 5 0 0]
[6 6 6 6 6 6 0]
[7 7 7 7 7 7 7]]
[[ 8 8 8 8 8 8 8 8 0 0 0]
[ 9 9 9 9 9 9 9 9 9 0 0]
[10 10 10 10 10 10 10 10 10 10 0]
[11 11 11 11 11 11 11 11 11 11 11]]
[[12 12 12 12 12 12 12 12 12 12 12 12 0 0 0]
[13 13 13 13 13 13 13 13 13 13 13 13 13 0 0]
[14 14 14 14 14 14 14 14 14 14 14 14 14 14 0]
[15 15 15 15 15 15 15 15 15 15 15 15 15 15 15]]
[[16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 0 0 0]
[17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 0 0]
[18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 0]
[19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19]]
对于输入的序列,有时候需要将get_next产生的序列元素分开,既可以使用 next_element[i] 这样的方式得到分片,也可以使用如下所述的 map 函数进行分片,然后调用
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
x = [[1, 0, 0],
[2, 3, 0],
[4, 5, 6],
[7, 8, 0],
[9, 0, 0],
[0, 1, 0]]
#tf.TensorShape([]) 表示长度为单个数字
#tf.TensorShape([None]) 表示长度未知的向量
padded_shapes=(
tf.TensorShape([None]),
tf.TensorShape([])
)
dataset = tf.data.Dataset.from_tensor_slices(x)
dataset = dataset.map(lambda x: (x, tf.reduce_sum(x)))
dataset = dataset.padded_batch(2, padded_shapes=padded_shapes)
iterator = dataset.make_one_shot_iterator()
sess = tf.Session()
try:
while True:
elem, value = iterator.get_next()
print("elem:", sess.run(elem))
except tf.errors.OutOfRangeError:
print("end")
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
x = [[1, 0, 0],
[2, 3, 0],
[4, 5, 6],
[7, 8, 0],
[9, 0, 0],
[0, 1, 0]]
#tf.TensorShape([]) 表示长度为单个数字
#tf.TensorShape([None]) 表示长度未知的向量
padded_shapes=(
tf.TensorShape([]),
tf.TensorShape([]),
tf.TensorShape([])
)
dataset = tf.data.Dataset.from_tensor_slices(x)
dataset = dataset.map(lambda x: [x[0], x[1], x[2]])
dataset = dataset.padded_batch(2, padded_shapes=padded_shapes)
iterator = dataset.make_one_shot_iterator()
sess = tf.Session()
try:
while True:
elem1, elem2, elem3 = iterator.get_next()
print("elem:", sess.run(elem1))
except tf.errors.OutOfRangeError:
print("end")