tensorflow
BJRSR
这个作者很懒,什么都没留下…
展开
-
tensorflow model的用法
import tensorflow as tffrom tensorflow.keras import layers, modelsmodels = models.Sequential()models.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)))models.add(layers.MaxPooling2D((2, 2)))models.add(layers.Conv.原创 2021-11-28 21:28:05 · 3103 阅读 · 0 评论 -
tensorflow中tf.nn.maxpool()
tf.nn.max_pool(feature_map, k_size, strides, padding)h : 需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch_size, height, width, channels]这样的shapek_size : 池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1strides : 窗口在每一个维度上.原创 2021-11-01 10:30:11 · 302 阅读 · 0 评论 -
tensorflow中tf.nn.conv2d()
tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)参数:input : 输入的要做卷积的图片,要求为一个张量,shape为 [ batch, in_height, in_width, in_channel ],其中batch为图片的数量,in_height 为图片高度,in_width 为图片宽度,in_channel 为图片的通道数,灰度图该值为1,彩色原创 2021-11-01 10:14:56 · 147 阅读 · 0 评论 -
tensorflow 1.14.0
创建迭代器可重新初始化迭代器可以通过多个不同的Dataset对象进行初始化.这些对象具有相同的结构(即每个组件具有相同类型和兼容形状)import tensorflow.compat.v1 as tftf.disable_v2_behavior()training_dataset = tf.data.Dataset.range(100).map( lambda x: x + tf.random_uniform([], -10, 10, tf.int64))validat...原创 2021-10-31 20:42:33 · 584 阅读 · 0 评论 -
tensorflow中tf.placeholder()
tf.placeholder()是用来分配变量在计算机中的内存,优化系统,不是为变量直接赋值等到session.run()后才会用feed_dict 字典对变量直接赋值。import tensorflow as tfimport numpy as npinput1 = tf.placeholder(tf.float32)input2 = tf.placeholder(tf.float32)output = tf.multiply(input1, input2)with tf.Ses原创 2021-10-29 22:04:09 · 353 阅读 · 0 评论 -
tensorflow中 batch, shuffle, repeat
notice:tensorflow 1.14.0版本import tensorflow.compat.v1 as tftf.disable_v2_behavior()import numpy as npimport osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'd = np.arange(0, 60).reshape([6, 10])data = tf.data.Dataset.from_tensor_slices(d) // numpy 转为 t原创 2021-10-29 16:00:22 · 321 阅读 · 0 评论 -
tensorflow 自定义层
import tensorflow as tffrom tensorflow.keras import layers, optimizers, datasets, Sequential, metricsfrom tensorflow import kerasdef preprocess(x, y): x = tf.cast(x, dtype=tf.float32) / 255 x = tf.reshape(x, [28 * 28]) y = tf.cast(y, dtype.原创 2021-07-20 10:18:42 · 552 阅读 · 0 评论 -
tensorflow 中loss
MSE:y = tf.constant([1, 2, 3, 0, 2])label = tf.one_hot(y, depth=4)logits = tf.random.normal([5, 4])loss1 = tf.reduce_mean(tf.square(logits - label))loss2 = tf.square(tf.norm(label-logits)) / 20loss3 = tf.reduce_mean(tf.losses.MSE(label, logits))原创 2021-07-19 11:13:21 · 453 阅读 · 0 评论 -
tensorflow中 全连接层
x = tf.random.normal([4, 784])net = tf.keras.layers.Dense(512)out = net(x)print(out.shape)print(net.kernel.shape)print(net.bias.shape)res:(4, 512)(784, 512)(512,)x = tf.random.normal([2, 3])model = keras.Sequential( [ keras.la.原创 2021-07-19 10:18:03 · 386 阅读 · 0 评论 -
tensorflow 等高线画法
def fun(x): ''' :param x:[b, 2]: :return: ''' z = tf.math.sin(x[..., 0]) + tf.math.sin(x[..., 1]) return zx = tf.linspace(0., 2*3.14, 500)y = tf.linspace(0., 2*3.14, 500)point_x, point_y = tf.meshgrid(x, y)print(point_x.shape.原创 2021-07-18 16:46:02 · 200 阅读 · 0 评论 -
tensorflow 中tf.scatter_nd
tf.scatter_nd: 把源数据中的元素根据标记的下标位置分散到新数组的位置中去如下:根据shape=【8】说明要新数组的shape为【8】,即【0,0,0,0,0,0,0,0,0,0】八个零。updates是源数组,indice是标记的位置下标,update根据indice把相应位置的元素分散到新数组中去indice = tf.constant([[4], [3], [1], [7]])updates = tf.constant([9, 10, 11, 12])shape = t.原创 2021-07-18 16:22:10 · 612 阅读 · 0 评论 -
tensorflow中tf.where用法
两种方法效果一样tf.where(condition):返回True元素的indexa = tf.random.normal([3, 3])print(a)mask = a > 0print(mask)b = tf.boolean_mask(a, mask)print(b)indices = tf.where(mask)print(indices)c = tf.gather_nd(a, indices)print(c)res:tf.Tensor([[ 0.2.原创 2021-07-18 11:33:11 · 765 阅读 · 0 评论 -
tensorflow tf.clip_by_value
tf.clip_by_value:作用:如图a = tf.constant(tf.range(9), shape=[9])print(a)b = tf.clip_by_value(a, 2, 8)print(b)res:tf.Tensor([2 2 2 3 4 5 6 7 8], shape=(9,), dtype=int32)tf.nn.relu:a = tf.constant(tf.range(9), shape=[9])a = a - 5print(a)b =原创 2021-07-18 11:12:10 · 284 阅读 · 0 评论 -
tensorflow中pad, tf.tile
tf.pad():作用:填充a = tf.reshape(tf.range(9), [3, 3])print(a)b = tf.pad(a, [[0, 0], [0, 0]])print(b)c = tf.pad(a, [[0, 1], [0, 1]])print(c)d = tf.pad(a, [[1, 1], [1, 1]])print(d)res:tf.Tensor([[0 1 2] [3 4 5] [6 7 8]], shape=(3, 3), dtype=原创 2021-07-18 10:40:25 · 216 阅读 · 0 评论 -
tensorflow中sort,argsort,topk
a = tf.random.shuffle(tf.range(5))print(a)b = tf.sort(a, direction='DESCENDING')print(b)idx = tf.argsort(a, direction='DESCENDING')print(idx)c = tf.gather(a, idx)print(c)res:tf.Tensor([1 0 3 2 4], shape=(5,), dtype=int32)tf.Tensor([4 3 2...原创 2021-07-17 10:30:26 · 985 阅读 · 0 评论 -
tensorflow tf.norm tf.reduce_max,min,mean, tf.argmin,...
a = tf.ones([2, 2])b = tf.norm(a)print(b)b = tf.sqrt(tf.reduce_sum(tf.square(a)))print(b)res:tf.Tensor(2.0, shape=(), dtype=float32)tf.Tensor(2.0, shape=(), dtype=float32)L1 Norm:a = tf.constant([1, 3, 2, 4], shape=[2,2])print(a)b = tf.no.原创 2021-07-17 09:13:34 · 495 阅读 · 0 评论 -
tensorflow tf.concat() tf.stack() unstack() split()
concat: 合并维度a = tf.random.uniform([4, 35, 8])b = tf.ones([2, 35, 8])c = tf.concat([a, b], axis=0)print(c.shape)b = tf.ones([4, 35, 8])d = tf.concat([a, b], axis=1)print(d.shape)res:(6, 35, 8)(4, 70, 8)stack: 创建新的维度a = tf.ones([4, 35, .原创 2021-07-17 08:12:23 · 182 阅读 · 0 评论 -
tensorflow 中数学运算
+-*/a = tf.fill([2, 2], 2.)# print(a)b = tf.ones([2, 2])# print(b)c = a + bprint(c.shape)d = a - bprint(d.shape)e = a * bprint(e.shape)f = a / bprint(f.shape)res:(2, 2)(2, 2)(2, 2)(2, 2)log, expb = tf.ones([2, 2])a = tf.math.l.原创 2021-07-15 22:18:06 · 190 阅读 · 0 评论 -
tensorflow中broadcast
a = tf.random.uniform([4, 32, 32, 3])b = tf.ones([3])c = a + bprint(c.shape)d = tf.random.uniform([32, 32, 1])e = a + dprint(e.shape)f = tf.random.uniform([4, 1, 1, 1])g = a + fprint(g.shape)res:(4, 32, 32, 3)(4, 32, 32, 3)(4, 32, 32, 3).原创 2021-07-15 21:52:48 · 268 阅读 · 0 评论 -
tensorflow中expand_dim()与squeeze()
a = tf.random.normal([4, 35, 8])b = tf.expand_dims(a, axis=0)print(b.shape)c = tf.expand_dims(a, axis=3)print(c.shape)d = tf.expand_dims(a, axis=-1)print(d.shape)e = tf.expand_dims(a, axis=-4)print(e.shape)res:(1, 4, 35, 8)(4, 35, 8, 1)(4, .原创 2021-07-15 21:32:54 · 196 阅读 · 0 评论 -
tensorflow 索引与切片 随机(2)
a = tf.random.uniform([4, 35, 8])idx = [2, 3]b = tf.gather(a, axis=0, indices=idx)print(b.shape)c = a[2:4, ...]print(c.shape)c = tf.gather(a, axis=0, indices=[2, 1, 0, 3])print(c.shape)d = tf.gather(a, axis=1, indices=[2, 3, 7, 9, 16])print(d.s.原创 2021-07-15 17:21:16 · 179 阅读 · 0 评论 -
tensorflow 的索引与切片(1)
x = tf.random.normal([4, 28, 28, 3])print(x[0].shape)print(x[1, 2].shape)print(x[1, 2, 3].shape)res:(28, 28, 3)(28, 3)(3,)切片原理:[A:B)a = tf.range(10)print(a)b = a[-1:]print(b)print(a[-2:])print(a[:2])print(a[:-1])res:tf.Tensor([0..原创 2021-07-15 17:06:44 · 166 阅读 · 0 评论 -
tensorflow 模拟loss
out = tf.random.uniform([4, 10])label = tf.range(4)label = tf.one_hot(label, depth=10)# print(label)loss = tf.keras.losses.mse(label, out)print(loss)loss = tf.reduce_mean(loss)print(loss)res:tf.Tensor([0.35132125 0.3203587 0.23202205 0.23277.原创 2021-07-15 15:58:49 · 155 阅读 · 0 评论 -
tensorflow中的序列随机打乱
random Permutationidx = tf.range(10)print(idx)idx = tf.random.shuffle(idx)print(idx)a = tf.random.normal([10, 784])b = tf.random.uniform([10], maxval=10, dtype=tf.int32)print(b)c = tf.gather(a, idx)d = tf.gather(b, idx) # print(c)print(d).原创 2021-07-15 15:51:58 · 1783 阅读 · 0 评论 -
tf.fill, tf.random.normal, tf.random.truncated_normal
tf.Tensor([[ 2.026736 0.40097588] [-0.02720833 1.5905641 ]], shape=(2, 2), dtype=float32)tf.fill:a = tf.fill([2, 3], 1)print(a)res:tf.Tensor([[1 1 1] [1 1 1]], shape=(2, 3), dtype=int32)tf.random.normal:a = tf.random.normal([2, 2...原创 2021-07-15 11:42:12 · 216 阅读 · 0 评论 -
tensorflow中zeros与zeros_like
a = tf.zeros([2, 3])print(a)b = tf.zeros([1])print(b)c = tf.zeros([2, 3, 3])print(c)d = tf.zeros_like(c)print(d)d = tf.zeros(d.shape)print(d)res:tf.Tensor([[0. 0. 0.] [0. 0. 0.]], shape=(2, 3), dtype=float32)tf.Tensor([0.], shape=(1,), .原创 2021-07-15 11:29:23 · 575 阅读 · 0 评论 -
tensorflow中的numpy与tensor互相转化
a = np.ones([3, 4])print(a)b = tf.convert_to_tensor(a, dtype=tf.int64)print(b)c = b.numpy()print(c)res:[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]tf.Tensor([[1 1 1 1] [1 1 1 1] [1 1 1 1]], shape=(3, 4), dtype=int64)[[1 1 1 1] [1 1 1 1] .原创 2021-07-15 11:17:13 · 1776 阅读 · 0 评论