keras slice layer 계층 구현 방식
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 층 의 실현 방식 은 바로 소 편 이 여러분 에 게 공유 한 모든 내용 입 니 다.여러분 께 참고 가 되 고 많은 응원 부 탁 드 리 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
KERA의 반복 레이어 시각적 안내서따라서 그림에서 우리는 크기가 4인 숨겨진 상태가 어떻게 RNN 블록 따라서 전체 문장에 대해 우리는 RNN 층에서 크기가 4인 벡터를 출력합니다. 그림에서 보듯이 우리는 무작위 입력 데이터를 만들었는데 그 중 한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.