2 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
應(yīng)該有兩件事要改變。
錯(cuò)誤案例 0。您不需要重塑層之間的流程。您可以None在第一個(gè)維度上使用來(lái)傳遞動(dòng)態(tài)批量大小。
錯(cuò)誤案例 1.您可以直接使用 newY 作為 NN 的輸出。您只使用 tf.Variable 來(lái)定義權(quán)重或偏差。
錯(cuò)誤案例 2.似乎 tensorflow 沒(méi)有梯度下降實(shí)現(xiàn),也tf.abs()沒(méi)有tf.gather()。對(duì)于回歸問(wèn)題,均方誤差通常就足夠了。
在這里,我如何重寫(xiě)您的代碼。我沒(méi)有你的 matlab 部分,所以我無(wú)法調(diào)試你的 python/matlab 接口:
模型:
def Model_FCL(inputX):
# Fully Connected Layer 1
fcW1 = tf.get_variable('w1', shape=[222, 1024], initializer=tf.initializer.truncated_normal())
fcb1 = tf.get_variable('b1', shape=[222], initializer=tf.initializer.truncated_normal())
# fcb1 = tf.get_variable('b1', shape=[None, 222], trainable=False, initializer=tf.constant_initializer(valueThatYouWant)) # if you want to fix your bias constant
fch1 = tf.nn.relu(tf.matmul(inputX, fcW1) + fcb1, name='relu1')
# Fully Connected Layer 2
fcW2 = tf.get_variable('w2', shape=[1024, 1024], initializer=tf.initializer.truncated_normal())
fcb2 = tf.get_variable('b2', shape=[222], initializer=tf.initializer.truncated_normal())
# fcb2 = tf.get_variable('b2', shape=[None, 222], trainable=False, initializer=tf.constant_initializer(valueThatYouWant)) # if you want to fix your bias constant
fch2 = tf.nn.relu(tf.matmul(fch1, fcW2) + fcb2, name='relu2')
# Output Layer
fcW3 = tf.get_variable('w3', shape=[1024, 222], initializer=tf.initializer.truncated_normal())
fcb3 = tf.get_variable('b3', shape=[222], initializer=tf.initializer.truncated_normal())
# fcb2 = tf.get_variable('b2', shape=[None, 222], trainable=False, initializer=tf.constant_initializer(valueThatYouWant)) # if you want to fix your bias constant
logits = tf.add(tf.matmul(fch2, fcW3), fcb3)
predictY = tf.nn.softmax(logits) #I'm not sure that it will learn if you do softmax then abs/MSE
return predictY, logits
圖形:
with myGraph.as_default():
# define input data & output data ???? ?? placeholder
# put None(dynamic batch size) not -1 at the first dimension so that you can change your batch size
x = tf.placeholder(tf.float32, shape=[None, 222], name='inputX') # Signal size = [1, 222]
y = tf.placeholder(tf.float32, shape=[None], name='trueY') # Float value size = [1]
...
predictY, logits = Model_FCL(x) # Predict Signal, size = [1, 222]
maxPredict = tf.argmax(predictY, 1, name='maxPredict') # Find max index of Predict Signal
tensorFreq = tf.convert_to_tensor(basicFreq, tf.float32)
newY = tf.gather(tensorFreq, maxPredict) * 60 # Find the value that corresponds to the Freq array index
loss = tf.losses.mean_squared_error(labels=y, predictions=newY) # maybe use MSE for regression problem
# loss = tf.abs(y - newY) # Calculate absolute (true Y - predict Y) #tensorflow doesn't have gradient descent implementation for tf.abs
opt = tf.train.AdamOptimizer(learning_rate=0.0001)
trainStep = opt.minimize(loss)

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
變量batchSignal
的類型或形狀似乎錯(cuò)誤。它必須是一個(gè) numpy 形狀的數(shù)組[1, 222]
。如果要使用一批大小為n × 222的示例,占位符x
的形狀應(yīng)為[None, 222]
和占位符y
形狀 [None]
。
順便說(shuō)一下,考慮使用tf.layers.dense
而不是顯式初始化變量并自己實(shí)現(xiàn)層。
添加回答
舉報(bào)