RL 문제에 대해 작업 중이며 모델 및 기타 매개 변수를 초기화하는 클래스를 만들었습니다. 코드는 다음과 같습니다.
class Agent:
def __init__(self, state_size, is_eval=False, model_name=""):
self.state_size = state_size
self.action_size = 20 # measurement, CNOT, bit-flip
self.memory = deque(maxlen=1000)
self.inventory = []
self.model_name = model_name
self.is_eval = is_eval
self.done = False
self.gamma = 0.95
self.epsilon = 1.0
self.epsilon_min = 0.01
self.epsilon_decay = 0.995
def model(self):
model = Sequential()
model.add(Dense(units=16, input_dim=self.state_size, activation="relu"))
model.add(Dense(units=32, activation="relu"))
model.add(Dense(units=8, activation="relu"))
model.add(Dense(self.action_size, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=0.003))
return model
def act(self, state):
options = self.model.predict(state)
return np.argmax(options[0]), options
한 번만 반복하여 실행하고 싶으므로 객체를 만들고 16
다음과 같은 길이의 벡터를 전달합니다 .
agent = Agent(density.flatten().shape)
state = density.flatten()
action, probs = agent.act(state)
그러나 다음과 같은 오류가 발생합니다.
AttributeError Traceback (most recent call last) <ipython-input-14-4f0ff0c40f49> in <module>
----> 1 action, probs = agent.act(state)
<ipython-input-10-562aaf040521> in act(self, state)
39 # return random.randrange(self.action_size)
40 # model = self.model()
---> 41 options = self.model.predict(state)
42 return np.argmax(options[0]), options
43
AttributeError: 'function' object has no attribute 'predict'
무엇이 문제입니까? 나는 다른 사람들의 코드도 이와 같이 확인 했고 내 생각도 매우 비슷하다고 생각합니다.
알려주세요.
편집하다:
의 인수를 Dense
에서 input_dim
로 input_shape
및 self.model.predict(state)
로 변경 했습니다 self.model().predict(state)
.
이제 shape의 한 입력 데이터에 대해 NN을 실행할 때 (16,1)
다음 오류가 발생합니다.
ValueError : 입력을 확인할 때 오류 발생 : density_1_input에 3 차원이 있어야하지만 모양 (16, 1)의 배열이 있습니다.
그리고 shape으로 실행 (1,16)
하면 다음 오류가 발생합니다.
ValueError : 입력을 확인할 때 오류 발생 : density_1_input에 3 차원이 있어야하지만 모양 (1, 16)의 배열이 있습니다.
이 경우 어떻게해야합니까?
마지막 코드 블록에서
def act(self, state):
options = self.model.predict(state)
return np.argmax(options[0]), options
self.model은 모델을 반환하는 함수이며 self.model (). predict (state) 여야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다