理解神經(jīng)網(wǎng)絡(luò),第二部分神經(jīng)網(wǎng)絡(luò)的類型
1. Feed-Forward Neural Networks (FNN)
· 最簡單的建筑。
· 信息向一個方向流動。
· 適合結(jié)構(gòu)化數(shù)據(jù)和分類。
· 范例用法 *信貸風(fēng)險評估。
2.卷積神經(jīng)網(wǎng)絡(luò)(CNN)
· 專門用于處理網(wǎng)格狀數(shù)據(jù)。
· 非常適合圖像和視頻處理。
· 使用卷積操作。
· 范例用法 :面部識別、醫(yī)學(xué)成像。
3.循環(huán)神經(jīng)網(wǎng)絡(luò)
· 處理順序數(shù)據(jù)。
· 有內(nèi)部記憶。
· 適合時間序列數(shù)據(jù)。
· 范例用法 ::股價預(yù)測。
4.長期短期記憶網(wǎng)絡(luò)
· 先進(jìn)的rnn變體。
· 更善于處理長期依賴關(guān)系。
· 包含特殊的記憶細(xì)胞。
· 范例用法 語言翻譯、語音識別。
神經(jīng)網(wǎng)絡(luò)是如何創(chuàng)建的?
建筑設(shè)計
1.簡單前進(jìn)神經(jīng)網(wǎng)絡(luò)(FNN)
這個模型是為數(shù)字分類等簡單任務(wù)設(shè)計的:
import tensorflow as tf
def create_basic_nn():
return tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
2.用于圖像處理的卷積神經(jīng)網(wǎng)絡(luò)
這一體系結(jié)構(gòu)是為圖像識別和處理等任務(wù)量身定制的:
def create_cnn():
return tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
神經(jīng)網(wǎng)絡(luò)如何學(xué)習(xí)?
學(xué)習(xí)過程
1. 前方通行證
def forward_pass(model, inputs):
# Make predictions
predictions = model(inputs)
return predictions
2. 計算錯誤
def calculate_loss(predictions, actual):
# Using categorical crossentropy
loss_object = tf.keras.losses.CategoricalCrossentropy()
loss = loss_object(actual, predictions)
return loss
3. 反傳播和優(yōu)化
# Complete training example
def train_model(model, x_train, y_train):
# Prepare data
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
# Compile model
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Train
history = model.fit(
x_train, y_train,
epochs=10,
validation_split=0.2,
batch_size=32,
callbacks=[
tf.keras.callbacks.EarlyStopping(patience=3),
tf.keras.callbacks.ReduceLROnPlateau()
])
return history
優(yōu)化技術(shù)
1. 批處理
· 迷你球梯度下降。
· 批正常化。
· 傾斜剪報。
2. 規(guī)則化
· 輟學(xué)。
· L1/L2 regularization.
· 數(shù)據(jù)增強。
業(yè)績指標(biāo)
def evaluate_model(model, x_test, y_test):
# Basic metrics
test_loss, test_accuracy = model.evaluate(x_test, y_test)
# Detailed metrics
predictions = model.predict(x_test)
from sklearn.metrics import classification_report, confusion_matrix
print(classification_report(y_test.argmax(axis=1), predictions.argmax(axis=1)))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test.argmax(axis=1), predictions.argmax(axis=1)))
共同挑戰(zhàn)和解決辦法
1. 過分的
· 癥狀:訓(xùn)練精度高,驗證精度低
· 解決辦法:
# Add regularization
tf.keras.layers.Dense(64, activation='relu',
kernel_regularizer=tf.keras.regularizers.l2(0.01))
# Add dropout
tf.keras.layers.Dropout(0.5)
2. 不合適的
· 癥狀:訓(xùn)練和驗證精度低
· 解決辦法:
o 增加模型容量
o 增加更多層
o 增加培訓(xùn)時間