keras slice layer 계층 구현 방식

주의 할 점:keras 의 각 층 의 입 출력 tensor 는 장 량 입 니 다.예 를 들 어 Tensor shape 는(N,H,W,C)이 고 tf 배경 에 대해 channels 입 니 다.last

Define a slice layer using Lamda layer
def slice(x, h1, h2, w1, w2):
 """ Define a tensor slice function
 """
 return x[:, h1:h2, w1:w2, :]
slice function 을 정의 한 후,lambda layer 를 이용 하여 정 의 된 네트워크 에 추가 합 니 다.

# Add slice layer
slice_1 = Lambda(slice, arguments={'h1': 0, 'h2': 6, 'w1': 0, 'w2': 6})(sliced)
# As for tensorfow backend, Lambda doesn't need output shape argument
slice_2 = Lambda(slice, arguments={'h1': 0, 'h2': 6, 'w1': 6, 'w2': 12})(sliced)
추가 지식:tensorflow 와 keras 장 량 절편(slice)
Notes
하나의 벡터를 두 부분 으로 나 누 려 면:작업 은 대략:

TensorFlow 에서 tf.slice 로 장 량 절편 을 실현 하고 Keras 에서 Lambda 층 을 사용자 정의 합 니 다.
TensorFlow
tf.slice(input_, begin, size, name=None)
input_:tf.tensor,작 동 되 는 tensor
begin:list,각 차원 의 시작 표시
size:list,각 차원 에서 얼마나 잘라 야 합 니까?

import tensorflow as tf

with tf.Session() as sess:
 a = tf.constant([1, 2, 3, 4, 5])
 b = tf.slice(a, [0], [2]) #        0   ,  2  
 c = tf.slice(a, [2], [3]) #        2   ,  3  
 print(a.eval())
 print(b.eval())
 print(c.eval())
출력
[1 2 3 4 5]
[1 2]
[3 4 5]
Keras

from keras.layers import Lambda
from keras.models import Sequential
import numpy as np

a = np.array([[1, 2, 3, 4, 5]])
model = Sequential([
 Lambda(lambda a: a[:, :2], input_shape=[5]) #       2  
])

print(model.predict(a))
출력
[[1. 2.]]
이상 의 이 keras slice layer 층 의 실현 방식 은 바로 소 편 이 여러분 에 게 공유 한 모든 내용 입 니 다.여러분 께 참고 가 되 고 많은 응원 부 탁 드 리 겠 습 니 다.

좋은 웹페이지 즐겨찾기