tensorflow中卷积操作的padding参数
filters = tf.reshape(tf.constant([1., 1., 2.]), (3, 1, 1))
x = tf.reshape(tf.constant([1., 1., 0., 2., 1.]), (1, 5, 1))
tf.nn.convolution(x, filters, strides=2, padding="VALID").numpy().squeeze()
# [2, 4]
tf.nn.convolution(x, filters, strides=2, padding="SAME").numpy().squeeze()
# [3, 5, 3]
filters = tf.reshape(tf.constant([1., 1., 2.]), (3, 1, 1))
x = tf.reshape(tf.constant([1., 1., 0., 2., 1., 2.]), (1, 6, 1))
tf.nn.convolution(x, filters, strides=2, padding="VALID").numpy().squeeze()
# [2, 4]
tf.nn.convolution(x, filters, strides=2, padding="SAME").numpy().squeeze()
# [2, 4, 3]
tensorflow中dataloader的一个小问题记录
import tensorflow as tf
tf_tensor = tf.random.normal((10, 4))
dataset = tf.data.Dataset.from_tensor_slices(tf_tensor) # staticmethod, 利用tensor构造
# 利用`tf.data.Dataset`对象构造, 返回为`tf.data.Dataset`的继承类
dataset = dataset.shuffle(5).batch(2)
for epoch in range(2):
for item in dataset:
print(item.shape) # 形状为(2, 4)
tf_tensor = tf.random.normal((10, 4))
dataset = MyDataset(tf_tensor, 2) # 随机丢弃前面0个或1个或2个样本
dataset = dataset.batch(2).shuffle(5)
for epoch in range(2):
for item in dataset: # 每次丢弃前面几个样本, 然后batch, 之后以batch为单位打乱顺序
print(item.shape) # 形状为(2, 4)
class MyDataset(object):
def __init__(self, tensor, num):
self.data = tensor
self.num = num
def _to_tf_dataset(self):
begin = np.random.choice(self.num, 1)[0]
return tf.data.Dataset.from_tensor_slices(self.data[begin:, ...])
def batch(self, batch_size):
return self._to_df_dataset().batch(batch_size)
def shuffle(self, buffer_size):
return self._to_df_dataset().shuffle(buffer_size)
def __iter__(self):
return iter(self._to_tf_dataset())
tf_tensor = tf.random.normal((10, 4))
dataset = MyDataset(tf_tensor, 2)
for epoch in range(2):
for item in dataset.batch(2).shuffle(5):
print(item.shape)
class MyDataset():
def __init__(self, tensor, num):
self.data = tf.data.Dataset.from_tensor_slices(tensor)
self.num = num
def skip(self):
begin = np.random.choice(self.num, 1)[0]
def batch(self, batch_size):
return self.batch(batch_size)
def shuffle(self, buffer_size):
return self._to_df_dataset().shuffle(buffer_size)
def __iter__(self):
return iter(self._to_tf_dataset())
def get_dataset(data, preprocessing):
"""
returns:
dataset (tf.data.Dataset): 归一化
"""