This repo contains the source code in my personal column (https://zhuanlan.zhihu.com/zhaoyeyu), implemented using Python 3.6. Including Natural Language Processing and Computer Vision projects, such as text generation, machine translation, deep convolution GAN and other actual combat code.
This is the warning of running this code :
D:\Program Files\Anaconda3\lib\site-packages\sklearn\model_selection_split.py:2010: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.
FutureWarning)
end = time.time()
elapsed = end - start
print ("Time taken: ", elapsed, "seconds.")
This is the error of running this code :
ResourceExhaustedError Traceback (most recent call last)
D:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, args)
1322 try:
-> 1323 return fn(args)
1324 except errors.OpError as e:
D:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in exit(self, type_arg, value_arg, traceback_arg)
472 compat.as_text(c_api.TF_Message(self.status.status)),
--> 473 c_api.TF_GetCode(self.status.status))
474 # Delete the underlying status object from memory otherwise it stays alive
Here's my code : from sklearn.preprocessing import LabelBinarizer n_class = 10 #总共10类 lb = LabelBinarizer().fit(np.array(range(n_class))) y_train = lb.transform(y_train) y_test = lb.transform(y_test)
from sklearn.model_selection import train_test_split
train_ratio = 0.8 xtrain, x_val, ytrain, y_val = train_test_split(x_train, y_train, train_size=train_ratio, random_state=123) img_shape = x_train.shape keep_prob = 0.6 epochs=5 batchsize=64 inputs = tf.placeholder(tf.float32, [None, 32, 32, 3], name='inputs') targets = tf.placeholder(tf.float32, [None, nclass], name='targets')
This is the warning of running this code : D:\Program Files\Anaconda3\lib\site-packages\sklearn\model_selection_split.py:2010: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified. FutureWarning)
第一层卷积加池化
32 x 32 x 3 to 32 x 32 x 64
conv1 = tf.layers.conv2d(inputs_, 64, (2,2), padding='same', activation=tf.nn.relu, kernel_initializer=tf.truncated_normal_initializer(mean=0.0, stddev=0.1))
32 x 32 x 64 to 16 x 16 x 64
conv1 = tf.layers.max_pooling2d(conv1, (2,2), (2,2), padding='same')
第二层卷积加池化
16 x 16 x 64 to 16 x 16 x 128
conv2 = tf.layers.conv2d(conv1, 128, (4,4), padding='same', activation=tf.nn.relu, kernel_initializer=tf.truncated_normal_initializer(mean=0.0, stddev=0.1))
16 x 16 x 128 to 8 x 8 x 128
conv2 = tf.layers.max_pooling2d(conv2, (2,2), (2,2), padding='same')
重塑输出
shape = np.prod(conv2.get_shape().as_list()[1:]) conv2 = tf.reshape(conv2,[-1, shape])
第一层全连接层
8 x 8 x 128 to 1 x 1024
fc1 = tf.contrib.layers.fully_connected(conv2, 1024, activation_fn=tf.nn.relu) fc1 = tf.nn.dropout(fc1, keep_prob)
第二层全连接层
1 x 1024 to 1 x 512
fc2 = tf.contrib.layers.fully_connected(fc1, 512, activation_fn=tf.nn.relu)
logits层
1 x 512 to 1 x 10
logits_ = tf.contrib.layers.fully_connected(fc2, 10, activationfn=None) logits = tf.identity(logits, name='logits')
cost & optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_withlogits(logits=logits, labels=targets_)) optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
accuracy
correctpred = tf.equal(tf.argmax(logits, 1), tf.argmax(targets_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy') import time save_model_path='./test_cifar' count = 0 start = time.time() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(epochs): for batch_i in range(img_shape[0]//batch_size-1): feature_batch = xtrain[batch_i batch_size: (batch_i+1)batch_size] label_batch = ytrain[batch_i batch_size: (batch_i+1)batch_size] trainloss, = sess.run([cost, optimizer], feeddict={inputs: featurebatch, targets: label_batch})
end = time.time() elapsed = end - start print ("Time taken: ", elapsed, "seconds.")
This is the error of running this code : ResourceExhaustedError Traceback (most recent call last) D:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, args) 1322 try: -> 1323 return fn(args) 1324 except errors.OpError as e:
D:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 1301 feed_dict, fetch_list, target_list, -> 1302 status, run_metadata) 1303
D:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in exit(self, type_arg, value_arg, traceback_arg) 472 compat.as_text(c_api.TF_Message(self.status.status)), --> 473 c_api.TF_GetCode(self.status.status)) 474 # Delete the underlying status object from memory otherwise it stays alive
ResourceExhaustedError: OOM when allocating tensor with shape[10000,32,32,64] [[Node: conv2d_13/Conv2D = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_inputs__2_0_0/_15, conv2d_12/kernel/read)]] [[Node: accuracy_6/_17 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_81_accuracy_6", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
ResourceExhaustedError Traceback (most recent call last)