Skip to content

Instantly share code, notes, and snippets.

@dharma6872
Last active January 27, 2021 08:57
Show Gist options
  • Save dharma6872/f7e119353c82000e326b0b7165b94db9 to your computer and use it in GitHub Desktop.
Save dharma6872/f7e119353c82000e326b0b7165b94db9 to your computer and use it in GitHub Desktop.
[reshape, squeeze, expand_dims 함수] #tensorflow

reshape 함수

import tensorflow as tf
import numpy as np

x = [i for i in range(10)]
print(x)

y = tf.reshape(x, shape=[-1, 5])
print(y)
print(y.shape)
# 배열을 reshape을 할 경우, 배열의 원소의 갯수는 [-1, 3]의 경우는 3의 배수의 원소의 갯수를 요구한다.
# 배열을 reshape을 할 경우, 배열의 원소의 갯수는 [-1, 2]의 경우는 2의 배수의 원소의 갯수를 요구한다.

x = np.array([
		[
			[1,1,1],
			[2,2,2]
		],
		[
			[3,3,3],
			[4,4,4]
		],
	])

print(x)
print(x.shape)

y = tf.reshape(x, shape=[-1])
print(y)
print(y.shape)

y = tf.reshape(x, shape=[-1, 3])
print(y)
print(y.shape)

y = tf.reshape(x, shape=[-1, 1, 3])
print(y)
print(y.shape)

squeeze 함수

import tensorflow as tf
import numpy as np

x = np.array([
	[0],[1],[2]
])

print(x)
print(x.shape)

y = tf.squeeze(x)
print(y)
print(y.shape)

x = np.array([
	[[1],[2]],
	[[3],[4]],
])
print(x)
print(x.shape)

y = tf.squeeze(x)
print(y)
print(y.shape)

squeeze 함수

import numpy as np
import tensorflow as tf

x = np.array([
	0, 1, 2
])
print("x.shape: ", x.shape)

y = tf.expand_dims(x, 0)
print(y)
print("y.shape: ", y.shape)
#(1,3)

y = tf.expand_dims(x, 1)
print(y)
print("y.shape: ", y.shape)
#(3,1)

x = np.array(
	[
		[1, 2],
		[3, 4]
	]
)
print("x.shape: ", x.shape)

y = tf.expand_dims(x, 0)
print(y)
print(y.shape)

y = tf.expand_dims(x, 1)
print(y)
print(y.shape)

y = tf.expand_dims(x, 2)
print(y)
print(y.shape)

y = tf.expand_dims(x, -1)
print(y)
print(y.shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment