SoFunction
Updated on 2024-10-29

Some notes on tensorflow's use of tensor ,,

It's been a while since I've used tensorflow, and now there are still some potholes in my experiments, mainly about tensor computation. tensorflow upgraded to version 1.0 is not compatible with previous versions, and all kinds of strange problems may occur.

1 Functions

Function usage before tensorflow 1.0: (concat_dim, values, name='concat'), the first parameter is the dimension of the concatenation, you can concatenate several vectors by the specified dimension.

As:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
# Connect according to dimension 0
(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# Connect according to dimension 1
(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

The role of the function is mainly to concatenate the vectors in the specified dimension, leaving the rest of the dimensions unchanged; and after version 1.0, the function's usage becomes:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
# Connect according to dimension 0
( [t1, t2],0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# Connect according to dimension 1
([t1, t2],1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

The location has changed and needs attention.

2 Functions

Usage: stack(values, axis=0, name="stack").

“”“Stacks a list of rank-R tensors into one rank-(R+1) tensor.

x = ([1, 4])
y = ([2, 5])
z = ([3, 6])
([x,y,z]) ==> [[1,4],[2,5],[3,6]]
([x,y,z],axis=0) ==> [[1,4],[2,5],[3,6]]
([x,y,z],axis=1) ==> [[1, 2, 3], [4, 5, 6]]

Turns a set of R-dimensional tensors into an R+1-dimensional tensor. Note that it has been turned into

Usage: reshape(tensor, shape, name=None):Mainly by changing the shape of the tensor, it can be changed from high dimension to low dimension or from low dimension to high dimension;

a = (initial_value=[[1,2,3],[4,5,6]]) ==> shape:[2,3]
b = (initial_value=[[[1,2,3],[4,5,6]],[[7,8,9],[1,0,2]]]) ==> shape:[2,2,3]

a_1 = (a,[2,1,1,3]) ==> [[[[1,2,3]]],[[[4,5,6]]]]
a_2 = (a,[2,1,3]) ==> [[[1,2,3]],[[4,5,6]]]
b_1 = (b,[2,2,1,3]) ==> [[[[1,2,3]],[[4,5,6]]],[[[7,8,9]],[[1,0,2]]]]

new_1 = ([b_1,a_1],1)
new_2 = (([b,a_2],1),[2,3,1,3])
"""
new_1:
[[[[1 2 3]]

 [[4 5 6]]

 [[1 2 3]]]


 [[[7 8 9]]

 [[1 0 2]]

 [[4 5 6]]]]
new_2;
[[[[1 2 3]]

 [[4 5 6]]

 [[1 2 3]]]


 [[[7 8 9]]

 [[1 0 2]]

 [[4 5 6]]]]

Additional knowledge:reshape(tensor,[1,-1]) and reshape(tensor,[-1,1]) in tensorflow

should be the same as reshape in python.

import tensorflow as tf
a = [[1,2],[3,4],[5,6]]
(a,[-1,1])
Out[13]: < 'Reshape_4:0' shape=(6, 1) dtype=int32>
((a,[-1,1]),[1,-1])
Out[14]: < 'Reshape_6:0' shape=(1, 6) dtype=int32>

(tensor,[-1,1]) turns the tensor into a one-dimensional column vector

(tensor,[1,-1]) turns the tensor into a one-dimensional row vector

Above this talk tensorflow use tensor some attention,, is what I share with you all the content, I hope to give you a reference, and I hope you support me more.