Tensorflow를 처음 사용하여 모양이 결합 된 (n,)
의 모양이 (n,1)
인 경우 문제가 있습니다. 나는 한 번에 하나의 예를 공급하여 확률 그라데이션 하강을 구현하기 위해 노력하고Tensorflow의 모양
if __name__ == '__main__':
trainSetX, trainSetY = utils.load_train_set()
# create placeholders & variables
X = tf.placeholder(tf.float32, shape=(num_of_features,))
y = tf.placeholder(tf.float32, shape=(1,))
W, b = initialize_params()
# predict y
y_estim = linear_function(X, W, b)
y_pred = tf.sigmoid(y_estim)
# set the optimizer
loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=y_pred)
loss_mean = tf.reduce_mean(loss)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=alpha).minimize(loss_mean)
# training phase
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for idx in range(num_of_examples):
cur_x, cur_y = trainSetX[idx], trainSetY[idx]
_, c = sess.run([optimizer, loss_mean], feed_dict={X: cur_x, y: cur_y})
:
나는이 코드를 가지고있다. 문제는 모양이 (num_of_features,)
인 데이터를 공급하는 것 같지만 다른 기능을 올바르게 사용하려면 (num_of_features,1)
이 필요합니다.
def linear_function(x, w, b):
y_est = tf.add(tf.matmul(w, x), b)
return y_est
오류이다 :이 함수 (Y)의 예측 연산에 관해서
예를 들어, 이전에 소정의 코드는 에러 발생 전 tf.reshape
를 사용하려고 시도 하였다
ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [1,3197], [3197].
X
및 y
으로이 문제를 해결했지만 다른 곳에서는 오류가 발생했습니다.
feed_dict={X: cur_x, y: cur_y}
의 데이터를 "올바른"모양으로 공급할 수 있습니까?
또는 이것을 올바르게 구현하는 방법은 무엇입니까?
감사합니다. 행렬 곱셈에 대한
을 제공
희망 나는 모양의 규칙을 알고있다. 문제는 내 모양이 다음과 같습니다 :'x_shape : (3197,); w_shape : (1, 3197)'. 'x = tf.reshape (x, [num_of_features, 1])'을 추가하면'matmul'가 정상적으로 작동합니다. 그런데 shape (1, 1)의'y_estim'을 가지고 있습니다. 그렇다면 (y가 1 인 경우) : y_estim = tf.reshape (y_estim, [1,]) .이 오류가 발생합니다. ValueError : shape 값을 공급할 수 없습니다() (1,) ' – Valeria
@Valeria Plz 편집 된 답변보기, 당신은'[1]'이 아니라'[1]'로 reshpae를 보았습니다. – LYu