Chào mọi người,
Mình đang học về tensorflow, máy học deep learning và đang làm 1 bài về phân lớp ảnh. Mình chỉ dùng mạng neural 1 lớp.
Hiện tại mình đang gặp lỗi như bên dưới:
> Traceback (most recent call last):
> File “draw_shape.py”, line 123, in
> batch_x, batch_y,batch_mask = batch_creator(batch_size, train_x.shape[0], ‘train’)
> File “draw_shape.py”, line 71, in batch_creator
> batch_x = eval(dataset_name + ‘_x’)[[batch_mask]].reshape(-1, input_num_units)
> ValueError: total size of new array must be unchanged
Đây là code của mình:
> from collections import Counter
> import os
> import numpy as np
> import pandas as pd
> from scipy.misc import imread
> import tensorflow as tf
> import cv2Preformatted text
> seed = 128
> rng = np.random.RandomState(seed)
> root_dir = "/home/trantrunghieu/lv"
> train = pd.read_csv(os.path.join(root_dir,"train", 'train.csv'))
> test = pd.read_csv(os.path.join(root_dir,"test",'test.csv'))
> train.head()
> temp = []
> for img_name in train.filename:
> image_path = os.path.join(root_dir, 'train', img_name)
> img = imread(image_path)
> img = img.astype('float32')
> img = tf.reshape(img,[-1])
> temp.append(img)
> train_x = np.stack(temp)
> for img_name in test.filename:
> image_path = os.path.join(root_dir, 'test', img_name)
> img = imread(image_path)
> img = img.astype('float32')
> img = tf.reshape(img,[-1])
>
> temp.append(img)
>
> test_x = np.stack(temp)
> train_y = train.label.values[0:]
> print(train_y)
> print (Counter(train_y))
> def dense_to_one_hot(labels_dense, num_classes=8):
> """Convert class labels from scalars to one-hot vectors"""
> num_labels = labels_dense.shape[0]
> index_offset = np.arange(num_labels) * num_classes
> labels_one_hot = np.zeros((num_labels, num_classes))
> labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
>
> return labels_one_hot
> def preproc(unclean_batch_x):
> """Convert values to range 0-1"""
> temp_batch = unclean_batch_x / unclean_batch_x.max()
>
> return temp_batch
> def batch_creator(batch_size, dataset_length, dataset_name):
> """Create batch with random samples and return appropriate format"""
> batch_mask = rng.choice(dataset_length, batch_size)
>
> batch_x = eval(dataset_name + '_x')[[batch_mask]].reshape(-1, input_num_units)
> batch_x = preproc(batch_x)
>
> if dataset_name == 'train':
> batch_y = eval(dataset_name).ix[batch_mask, 'label'].values
> batch_y = dense_to_one_hot(batch_y)
>
> return batch_x, batch_y,batch_mask
> input_num_units = 16384
> output_num_units = 8
> x = tf.placeholder(tf.float32, [None, input_num_units])
> y = tf.placeholder(tf.float32, [None, output_num_units])
> epochs = 5
> batch_size = 128
> learning_rate = 0.01
> W = tf.Variable(tf.zeros([input_num_units, output_num_units]))
> b = tf.Variable(tf.zeros([output_num_units]))
> output_layer = tf.matmul(x, W) + b
> cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output_layer, y))
> optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
> init = tf.global_variables_initializer()
> batch_mask = rng.choice(train_x.shape[0], batch_size)
> print("Batch_Mask: ",batch_mask)
> with tf.Session() as sess:
> sess.run(init)
>
> for epoch in range(epochs):
> avg_cost = 0
> total_batch = int(train.shape[0]/batch_size)
> print(total_batch)
> for i in range(total_batch):
> batch_x, batch_y,batch_mask = batch_creator(batch_size, train_x.shape[0], 'train')
> _, c = sess.run([optimizer, cost], feed_dict = {x: batch_x, y: batch_y})
> avg_cost += c / total_batch
> print "Epoch:", (epoch+1), "cost =", "{:.5f}".format(avg_cost)
>
> print "\nTraining complete!"