import numpy as np import tensorflow as tf from SpectralLayer import Spectral mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 flat_train = np.reshape(x_train, [x_train.shape[0], 28*28]) flat_test = np.reshape(x_test, [x_test.shape[0], 28*28]) model = tf.keras.Sequential() model.add(tf.keras.layers.Input(shape=(28*28), dtype='float32')) model.add(Spectral(2000, is_base_trainable=True, is_diag_trainable=True, diag_regularizer='l1', use_bias=False, activation='tanh')) model.add(Spectral(10, is_base_trainable=True, is_diag_trainable=True, use_bias=False, activation='softmax')) opt = tf.keras.optimizers.Adam(learning_rate=0.003) model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.summary() epochs = 10 history = model.fit(flat_train, y_train, batch_size=1000, epochs=epochs) print('Evaluating on test set...') testacc = model.evaluate(flat_test, y_test, batch_size=1000) eig_number = model.layers[0].diag.numpy().shape[0] + 10 print('Trim Neurons based on eigenvalue ranking...') cut = [0.0, 0.001, 0.01, 0.1, 1] · for c in cut: zero_out = 0 for z in range(0, len(model.layers) - 1): # put to zero eigenvalues that are below threshold diag_out = model.layers[z].diag.numpy() diag_out[abs(diag_out) < c] = 0 model.layers[z].diag = tf.Variable(diag_out) zero_out = zero_out + np.count_nonzero(diag_out == 0) model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy']) testacc = model.evaluate(flat_test, y_test, batch_size=1000, verbose=0) trainacc = model.evaluate(flat_train, y_train, batch_size=1000, verbose=0) print('Test Acc:', testacc[1], 'Train Acc:', trainacc[1], 'Active Neurons:', 2000-zero_out)
时间: 2024-04-21 08:23:44 浏览: 249
Mnist_keras.py.zip_keras_kerasmnist_py神经网络_神经网络_神经网络 keras
这段代码是一个使用Spectral Layer的神经网络模型对MNIST数据集进行训练和评估的示例。首先,代码加载了MNIST数据集,并将像素值归一化到0到1之间。然后,代码定义了一个包含两个Spectral层的Sequential模型。每个Spectral层都具有不同的参数设置,包括基向量和对角矩阵的可训练性、正则化方式、激活函数等。模型使用Adam优化器进行编译,损失函数为sparse_categorical_crossentropy,评估指标为准确率。接下来,代码训练模型并评估在测试集上的性能。最后,代码根据特定的阈值对特征进行修剪,并输出修剪后的模型的测试准确率、训练准确率和活跃神经元数。
请问有什么问题我可以帮助您解答的吗?
阅读全文