我创建了多个配置文件功能,只允许在训练和测试期间使用一定比例的神经元或通道。其目的是在计算范围内动态扩展,以便在训练和推理期间降低计算复杂度,并提高http://www.eecs.harvard.edu/~htk/publication/2017-icmla-mcdanel-teerapittayanon-kung.pdf中报告的能效和内存利用率。我最大的挑战是如何计算每个配置文件功能(0-20%,20%-40%和40%-100%)中每个配置文件系数的损失和优化器,以便明智地训练网络配置文件。以下是第一个配置文件制定代码的一部分(0-20%):
def linear_func(n):
return[np.float32(1.0 - 1.0 * i/n) for i in range(1, n + 1)]
L = linear_func(100)
def linear_profile(lp, n_1):
p_L = tf.constant(L, shape = [1, 100])
L_11 = tf.constant(1.0, shape = [1, int(np.round((lp) * n_1))])
L_12 = tf.zeros(shape = [1, int(np.round((1 - lp) * n_1))])
L1 = tf.concat((L_11, L_12), axis = 1)
p_L1 = tf.multiply(L1, p_L)
return p_L1
# Creating Multiple Profile
pc1 = np.linspace(0, 0.2, 10)
pc2 = np.linspace(0.2, 0.4, 10)
pc3 = np.linspace(0.4, 1.0, 10)
profile_1 = []
profile_2 = []
profile_3 = []
for j in pc1:
p_L1 = linear_profile(j, 100)
profile_11 = tf.stack(p_L1, axis = 0)
profile_1.append(profile_11)
profile1 = tf.convert_to_tensor(profile_1, dtype=tf.float32)
for j in pc2:
p_L1 = linear_profile(j, 100)
profile_22 = tf.stack(p_L1, axis = 0)
profile_2.append(profile_22)
profile2 = tf.convert_to_tensor(profile_2, dtype=tf.float32)
for j in pc3:
p_L1 = linear_profile(j, 100)
profile_33 = tf.stack(p_L1, axis = 0)
profile_3.append(profile_33)
profile3 = tf.convert_to_tensor(profile_3, dtype=tf.float32)
def mlp_1(x, profile_type):
logist_t = []
for j in range(len(pc1)):
Z_ML11 = tf.add(tf.matmul(x, parameters['W1']), parameters['b1'])
A_ML11 = tf.nn.relu(Z_ML11)
P_ML11 = tf.multiply(profile_type[j], A_ML11)
Z_ML12 = tf.add(tf.matmul(P_ML11, parameters['W2']), parameters['b2'])
A_ML12 = tf.nn.relu(Z_ML12)
P_ML12 = tf.multiply(profile_type[j], A_ML12)
out_layer = tf.add(tf.matmul(P_ML12, parameters['W3']), parameters['b3'])
logist_t.append(out_layer)
return logist_t
logist_1 = mlp_1(X, profile_1)
logist_2 = mlp_1(X, profile_2)
logist_3 = mlp_1(X, profile_3)
def optimize_param(logits):
for j in range(len(pc1)):
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits[j], labels = Y))
optimizer = tf.train.MomentumOptimizer(learning_rate = learning_rate, momentum = 0.98).minimize(loss_op)
return loss_op, optimizer
# Initializing the variables
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for j in range(0,3):
if j == 0:
for r in range(len(logist_1)):
loss_op_1,optimizer_1 = optimize_param(logist_1[r])
# Training Loop
cost_1 = []
for epoch in range(training_epochs):
avg_cost1 = 0.
total_batch = int(mnist.train.num_examples/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
_, c_1 = sess.run([loss_op_1, optimizer_1], feed_dict = {X: batch_x, Y: batch_y})
# Compute average losses
avg_cost1 += c_1 / total_batch
cost_1.append(avg_cost1)
train_accuracy_11 = []
test_accuracy_11 = []
for k in range(len(logist_1)):
if i % 5000 == 0:
pred_1 = tf.nn.softmax(logits_1[k]) # Apply softmax to logits
correct_prediction_1 = tf.equal(tf.argmax(pred_1, 1), tf.argmax(Y, 1))
accuracy_1 = tf.reduce_mean(tf.cast(correct_prediction_1, "float"))
trian_accuracy_1 = accuracy_1.eval({X: mnist.train.images, Y: mnist.train.labels})
test_accuracy_1 = accuracy_1.eval({X: mnist.test.images, Y: mnist.test.labels})*100
train_accuracy_11.append(train_accuracy_1)
test_accuracy_11.append(test_accuracy_1)
# Display logs per epoch step
for k in range(len(logist_1)):
if epoch % display_step == 0:
print("Epoch_1:", '%03d' % (epoch + 1), "cost = {:.9f}".format(avg_cost1[k]))
for l in pc1:
if l % display_step == 0:
print("Percentage_Profile_1:", "Test Accuracy_1 = {}".format(test_accuracy_11)
运行代码时,出现以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-54-2a0698ea5834> in <module>()
120 batch_x, batch_y = mnist.train.next_batch(batch_size)
121 # Run optimization op (backprop) and cost op (to get loss value)
--> 122 _, c_1 = sess.run([loss_op_1, optimizer_1], feed_dict = {X: batch_x, Y: batch_y})
123
124 # Compute average losses
/Users/Abdullahi/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/Users/Abdullahi/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
967
968 # Create a fetch handler to take care of the structure of fetches.
--> 969 fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
970
971 # Run request and get response.
/Users/Abdullahi/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, graph, fetches, feeds)
406 """
407 with graph.as_default():
--> 408 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
409 self._fetches = []
410 self._targets = []
/Users/Abdullahi/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in for_fetch(fetch)
228 elif isinstance(fetch, (list, tuple)):
229 # NOTE(touts): This is also the code path for namedtuples.
--> 230 return _ListFetchMapper(fetch)
231 elif isinstance(fetch, dict):
232 return _DictFetchMapper(fetch)
/Users/Abdullahi/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, fetches)
335 """
336 self._fetch_type = type(fetches)
--> 337 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
338 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
339
/Users/Abdullahi/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in for_fetch(fetch)
225 if fetch is None:
226 raise TypeError('Fetch argument %r has invalid type %r' %
--> 227 (fetch, type(fetch)))
228 elif isinstance(fetch, (list, tuple)):
229 # NOTE(touts): This is also the code path for namedtuples.
TypeError: Fetch argument None has invalid type <type 'NoneType'>