我正在為我的最后一層尋找正確的激活函式,并可能為訓練尋找正確的損失函式。
在輸入時,我的模型從 0 到 7 獲取符號。我想訓練我的模型以了解哪個數字屬于哪個編碼。
在輸出中,我想得到三個代表編碼的數字。
我認為我對模型的預測不太合適。
我給你的代碼:
import numpy as np
import tensorflow as tf
from tensorflow import keras
# Creating train data:
trainSym = np.random.randint(low=0,high=8,size=5000)
# Bit labeling for my symbols:
bit_labels = np.zeros((8,3))
# Labeling:
for j in range(8):
for i in range(3):
# Return the binary representation of the input number as a string.
bit_labels[j,i] = np.binary_repr(j,width=3)[i]
print(bit_labels)
# Converting to my train labels:
trainLabels = tf.gather(bit_labels,trainSym)
print(trainLabels)
# check the correct order:
for i in range(8):
print(trainSym[i],'|',trainLabels[i])
# Creating the model:
input_signal = keras.Input(shape=(),dtype=tf.int32)
one_hot = tf.one_hot(input_signal,depth=8)
encoded = keras.layers.Dense(16,activation='relu')(one_hot)
encoded1 = keras.layers.Dense(2,activation='relu')(encoded)
decoded = keras.layers.Dense(2,activation='relu')(encoded1)
decoded1 = keras.layers.Dense(16,activation='relu')(decoded)
decoded2 = keras.layers.Dense(3,activation='sigmoid')(decoded1)
model = keras.Model(input_signal,decoded2)
# structure from our model:
print(model.summary())
# Optimizer and loss function:
lr = 0.001
adam = keras.optimizers.Adam(learning_rate = lr)
model.compile(optimizer=adam,loss="binary_crossentropy")
# Callbacks:
early_stopping = keras.callbacks.EarlyStopping(monitor='loss',
patience=3,
restore_best_weights=True)
# Training:
history = model.fit(trainSym,trainLabels,
epochs=10,
batch_size=256,
callbacks=[early_stopping])
# Evaluation:
test_symbols= np.random.randint(low=0,high=8,size=100)
test_labels = tf.gather(bit_labels,test_symbols)
pred_final_signal = model.predict(test_symbols,batch_size=128)
pred_output = tf.cast(tf.less(pred_final_signal,0.5),tf.int8)
# Calculate the number of errors:
no_error = tf.equal(pred_output,tf.cast(test_labels,tf.int8))
no_error = tf.reduce_mean(tf.cast(no_error, tf.float32))
print(no_error)
有人知道我的 pred_final_signal 中的數字是什么意思嗎?
非常感謝 :-)
uj5u.com熱心網友回復:
嘗試使用np.where閾值來理解您的預測:
import numpy as np
import tensorflow as tf
from tensorflow import keras
# Creating train data:
trainSym = np.random.randint(low=0,high=8,size=5000)
# Bit labeling for my symbols:
bit_labels = np.zeros((8,3))
# Labeling:
for j in range(8):
for i in range(3):
# Return the binary representation of the input number as a string.
bit_labels[j,i] = np.binary_repr(j,width=3)[i]
print(bit_labels)
# Converting to my train labels:
trainLabels = tf.gather(bit_labels,trainSym)
print(trainLabels)
# check the correct order:
for i in range(8):
print(trainSym[i],'|',trainLabels[i])
# Creating the model:
input_signal = tf.keras.Input(shape=(),dtype=tf.int32)
one_hot = tf.one_hot(input_signal,depth=8)
encoded = tf.keras.layers.Dense(32,activation='relu')(one_hot)
encoded1 = tf.keras.layers.Dense(16,activation='relu')(encoded)
encoded1 = tf.keras.layers.Dense(8,activation='relu')(encoded1)
decoded = tf.keras.layers.Dense(8,activation='relu')(encoded1)
decoded1 = tf.keras.layers.Dense(16,activation='relu')(decoded)
decoded1 = tf.keras.layers.Dense(32,activation='relu')(decoded1)
decoded2 = tf.keras.layers.Dense(3, activation='sigmoid')(decoded1)
model = tf.keras.Model(input_signal,decoded2)
# structure from our model:
print(model.summary())
# Optimizer and loss function:
lr = 0.001
adam = keras.optimizers.Adam(learning_rate = lr)
model.compile(optimizer=adam,loss="binary_crossentropy")
# Callbacks:
early_stopping = keras.callbacks.EarlyStopping(monitor='loss',
patience=3,
restore_best_weights=True)
# Training:
history = model.fit(trainSym,trainLabels,
epochs=15,
batch_size=256,
callbacks=[early_stopping])
# Evaluation:
test_symbols= np.random.randint(low=0,high=8,size=50)
pred_final_signal = model.predict(test_symbols,batch_size=50)
print(test_symbols)
pred_final_signal = np.where(pred_final_signal>0.5,1,0)
print(pred_final_signal)
[6 2 6 5 7 7 0 2 6 3 6 5 3 4 1 0 7 0 0 4 0 4 7 4 0 6 2 4 7 7 0 3 1 5 7 2 6
3 1 1 1 5 3 2 6 0 0 2 4 7]
[[1 1 0]
[0 1 0]
[1 1 0]
[1 0 1]
[1 1 1]
[1 1 1]
[0 0 0]
[0 1 0]
[1 1 0]
[0 1 1]
[1 1 0]
[1 0 1]
[0 1 1]
[1 0 0]
[0 0 1]
[0 0 0]
[1 1 1]
[0 0 0]
[0 0 0]
[1 0 0]
[0 0 0]
[1 0 0]
[1 1 1]
[1 0 0]
[0 0 0]
[1 1 0]
[0 1 0]
[1 0 0]
[1 1 1]
[1 1 1]
[0 0 0]
[0 1 1]
[0 0 1]
[1 0 1]
[1 1 1]
[0 1 0]
[1 1 0]
[0 1 1]
[0 0 1]
[0 0 1]
[0 0 1]
[1 0 1]
[0 1 1]
[0 1 0]
[1 1 0]
[0 0 0]
[0 0 0]
[0 1 0]
[1 0 0]
[1 1 1]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479252.html
上一篇:與Arm上的TensorFlow鏈接:GLIBC_2.32、GLIBC_2.33、GLIBC_2.34-哪一個?
