簡體   English   中英

重量變量Tensorflow的形狀

[英]Shape of weight variable Tensorflow

我正在嘗試學習如何使用tf.data.TFRecordDataset()但對此感到困惑。 我有一個tfrecords文件,其中包含我的圖像(24K)和標簽,並且我已將所有圖像的大小調整為100x100x3。

首先,我用tf.data.TFRecordDataset加載了tfrecords文件,並解析了數據和其他內容,正如您在代碼中看到的那樣。 然后,我編寫了一個簡單的模型來學習tfrecord文件的使用,但是在嘗試運行時遇到了卡住並出現錯誤。 我已經在互聯網上進行搜索,但找不到任何答案。

這是我的代碼: Train.py

import tensorflow as tf
import numpy as np
import os
import  glob
NUM_EPOCHS = 10
batch_size = 128
def _parse_function(example_proto):
  features = {"train/image": tf.FixedLenFeature((), tf.string, default_value=""),
            "train/label": tf.FixedLenFeature((), tf.int64, default_value=0)}
  parsed_features = tf.parse_single_example(example_proto, features)
  image = tf.decode_raw(parsed_features['train/image'], tf.float32)
  label = tf.cast(parsed_features['train/label'], tf.int32)
  image = tf.reshape(image, [100, 100, 3])
  image = tf.reshape(image, [100*100*3])

  return image, label

filename = 'train_data1.tfrecords'
dataset = tf.data.TFRecordDataset(filename)
dataset = dataset.map(_parse_function)
#dataset = dataset.repeat(NUM_EPOCHS)
dataset = dataset.batch(batch_size=batch_size)

iterator = dataset.make_initializable_iterator()
image, label = iterator.get_next()


w = tf.get_variable(name='Weights',shape= [30000,3] , initializer=tf.random_normal_initializer(0, 0.01))
b = tf.get_variable(name='Biases', shape= [1, 3],initializer=tf.zeros_initializer())

logits = tf.matmul(image, w) + b

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=label, name='Entropy'), name='loss')

optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)

preds = tf.nn.softmax(logits)
correct_preds = tf.equal(tf.argmax(preds, axis=1), tf.argmax(label, axis=1))
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))



with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(2):
        sess.run(iterator.initializer)
        total_loss = 0
        n_batches = 0
        try:
            while True:
                _, l = sess.run([optimizer, loss])
                total_loss += l
                n_batches +=1
        except tf.errors.OutOfRangeError:
            pass
        print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))

這是圖像的輸出:

<tf.Tensor 'IteratorGetNext:0' shape=(?, 30000) dtype=float32>

標簽是:

<tf.Tensor 'IteratorGetNext:1' shape=(?,) dtype=int32>

這次我得到了這個錯誤:

logits和標簽的大小必須相同:logits_size = [128,3] labels_size = [1,128]。

當我使用label = tf.reshape(label,[128,1])將label(我認為,我在這里做錯了)的label = tf.reshape(label,[128,1])為[128,1]時label = tf.reshape(label,[128,1])會出現以下錯誤:

輸入尺寸為[128,1],[2]且輸入張量計算為部分形狀的“梯度/熵/ Reshape_grad /變形”(op:“變形”)的尺寸必須被3整除,但為128。輸入[1] = [?,3]。

我正在嘗試將我的3類分類:自行車0,公共汽車1,汽車2。

這是我讀取圖像並將其標記到tfrecords tfrecordWriter.py的代碼

shuffle_data = True
cat_dog_train_path = './Train/*.jpg'
addrs = glob.glob(cat_dog_train_path)
labels = [0 if 'bike' in addr else 1 if 'bus' in addr else 2 for addr in addrs]

if shuffle_data:
    c = list(zip(addrs, labels))
    shuffle(c)
    addrs, labels = zip(*c)


train_addrs = addrs[:]
train_labels = labels[:]
train_shape = []
def load_image(addr):
    img = cv2.imread(addr)
    img = cv2.resize(img, (100, 100), interpolation=cv2.INTER_AREA)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img.astype(np.float32)
    return img


def _int64_feature(value):
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


train_filename = 'train_data1.tfrecords'
# open the TFRecords file
writer = tf.python_io.TFRecordWriter(train_filename)
for i in range(len(train_addrs)):
    print ('Train data: {}/{}'.format(i+1, len(train_addrs)))
    sys.stdout.flush()
    img = load_image(train_addrs[i])
    label = train_labels[i]
    feature = {'train/label': _int64_feature(label),
               'train/image': _bytes_feature(tf.compat.as_bytes(img.tostring()))}
    example = tf.train.Example(features=tf.train.Features(feature=feature))
    writer.write(example.SerializeToString())

writer.close()
sys.stdout.flush()

謝謝

問題出自此行:

    w = tf.get_variable(name='Weights',shape= [None, 100, 100, 3] , initializer=tf.random_normal_initializer(0, 0.01))

您指定了權重的形狀為shape=[None,100,100,3] ,而Tensorflow無法處理。 由於錯誤顯示“必須完全定義新變量的形狀(權重)”,因此權重的尺寸不能為“ None 在我看來,您將輸入張量的形狀與權重張量的形狀混淆了。 看起來您還沒有在任何地方展平圖像,因此您的模型沒有任何意義。 您在哪里:

    logits = tf.matmul(image, w) + b

看來您正在嘗試將此問題視為簡單的邏輯回歸,將圖像的像素作為單個特征。 可以采用第一種方法(但是通常會在圖像上使用卷積網絡),但是實際上必須將圖像展平為shape=[batchsize,30000]shape=[batchsize,30000] ,然后權重將變為shape=[30000,num_labels]這樣在矩陣乘法結束時,您將最終得到形狀為shape=[batchsize,num_labels] 根據您的代碼編寫方式,我覺得您對要完成的任務背后的數學或運算有一些基本的誤解。 也許回顧一下您到底想做什么。

編輯:這里的問題是對該算法正在做的一個基本的誤解。 該算法產生3個輸出,因此標簽必須具有3個對應的標簽才能匹配3個輸出。 您的標簽不能只是一個數字-0,1或2,具體取決於類別。 您的標簽必須是3個數字,每個數字都告訴您圖像是否在該類別中。 換句話說,您必須使用3分量(單熱)矢量而不是1分量編號來標記圖像。 每個圖像的標簽應如下所示:

[1,0,0] - bike
[0,1,0] - bus
[0,0,1] - car

所以你的標簽的形狀(128,3)應該是一樣的輸出形狀(128,3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM