tensorflow转化成matlab,将tensorflow导出到matlab

景唯
2023-12-01

首先,我想我只需在屏幕上打印变量并将其复制到excel文件中,然后将其导出为.csv文件,以便在MATLAB中使用它们。在

但因为有太多的重量,这不是一个可行的选择。所以我用numpy将它们保存为.csv文件。一切都很好,但当我在Matlab中运行这个模型时,它似乎不起作用。当然有可能是我在Matlab代码中出错了。在

但是我注意到我的代码所打印的值与写入.csv文件的值不一样。在

由于我不熟悉tensorflow和python,所以我通过“缝合”不同的示例来编写代码。在

以下是tensorflow代码:#BREZ KONVOLUCIJE

import os

import os.path

import shutil

import tensorflow as tf

import numpy as np

LOGDIR = "/home/ubuntu/ml/tf-dev-summit-tensorboard-tutorial-master/mnist_NOCONV/"

LABELS = os.path.join(os.getcwd(), "labels_1024.tsv")

SPRITES = os.path.join(os.getcwd(), "sprite_1024.png")

### MNIST EMBEDDINGS ###

mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + "data", one_hot=True)

### Get a sprite and labels file for the embedding projector ###

if not (os.path.isfile(LABELS) and os.path.isfile(SPRITES)):

print("Necessary data files were not found. Run this command from inside the "

"repo provided at "

"https://github.com/dandelionmane/tf-dev-summit-tensorboard-tutorial.")

exit(1)

def fc_layer(input, size_in, size_out, name="fc"):

with tf.name_scope(name):

w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W")

b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")

act = tf.matmul(input, w) + b

tf.summary.histogram("weights", w)

tf.summary.histogram("biases", b)

tf.summary.histogram("activations", act)

return act

def mnist_model(learning_rate, use_two_fc, use_two_conv, hparam):

tf.reset_default_graph()

sess = tf.Session()

# Setup placeholders, and reshape the data

x = tf.placeholder(tf.float32, shape=[None, 784], name="x")

x_image = tf.reshape(x, [-1, 28, 28, 1])

tf.summary.image('input', x_image, 10)

y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")

# FC layer 1

w1 = tf.Variable(tf.truncated_normal([784, 1024], stddev=0.1), name="W1")

b1 = tf.Variable(tf.constant(0.1, shape=[1024]), name="B1")

act1 = tf.matmul(x, w1) + b1

tf.summary.histogram("weights", w1)

tf.summary.histogram("biases", b1)

tf.summary.histogram("activations", act1)

relu = tf.nn.relu(act1)

embedding_input = relu

embedding_size = 1024

tf.summary.histogram("fc1/relu", relu)

# FC layer 2

w2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name="W2")

b2 = tf.Variable(tf.constant(0.1, shape=[10]), name="B2")

logits = tf.matmul(relu, w2) + b2

tf.summary.histogram("weights", w2)

tf.summary.histogram("biases", b2)

tf.summary.histogram("activations", logits)

#if use_two_fc:

# fc1 = fc_layer(x, 784, 1024, "fc1")

# relu = tf.nn.relu(fc1)

# embedding_input = relu

#tf.summary.histogram("fc1/relu", relu)

# embedding_size = 1024

#logits = fc_layer(relu, 1024, 10, "fc2")

with tf.name_scope("xent"):

xent = tf.reduce_mean(

tf.nn.softmax_cross_entropy_with_logits(

logits=logits, labels=y), name="xent")

tf.summary.scalar("xent", xent)

with tf.name_scope("train"):

train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)

with tf.name_scope("accuracy"):

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

tf.summary.scalar("accuracy", accuracy)

summ = tf.summary.merge_all()

embedding = tf.Variable(tf.zeros([1024, embedding_size]), name="test_embedding")

assignment = embedding.assign(embedding_input)

saver = tf.train.Saver()

sess.run(tf.global_variables_initializer())

writer = tf.summary.FileWriter(LOGDIR + hparam)

writer.add_graph(sess.graph)

config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()

embedding_config = config.embeddings.add()

embedding_config.tensor_name = embedding.name

embedding_config.sprite.image_path = SPRITES

embedding_config.metadata_path = LABELS

# Specify the width and height of a single thumbnail.

embedding_config.sprite.single_image_dim.extend([28, 28])

tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config)

for i in range(2001):

batch = mnist.train.next_batch(100)

if i % 5 == 0:

[train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]})

writer.add_summary(s, i)

if i % 500 == 0:

sess.run(assignment, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]})

saver.save(sess, os.path.join(LOGDIR, "model.ckpt"), i)

sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})

# Get the values of variables

w1_val, b1_val, w2_val, b2_val = sess.run([w1, b1, w2, b2])

np.savetxt("w1.csv", w1_val, delimiter=",")

np.savetxt("b1.csv", b1_val, delimiter=",")

np.savetxt("w2.csv", w2_val, delimiter=",")

np.savetxt("b2.csv", b2_val, delimiter=",")

tvars = tf.trainable_variables()

tvars_vals = sess.run(tvars)

print("\n \n \n")

for var, val in zip(tvars, tvars_vals):

print(var.name, val) # Prints the name of the variable alongside its value.

print("\n \n \n")

def main():

# You can try adding some more learning rates

learning_rate = 1E-4

use_two_fc = True

hparam = "OCR_2FC_NOCONV"

print('Starting run for %s' % hparam)

# Actually run with the new settings

mnist_model(learning_rate, use_two_fc, False, hparam)

print('Done training!')

print('Run `tensorboard --logdir=%s --host localhost --port 8088` to see the results.' % LOGDIR)

if __name__ == '__main__':

main()

以及MATLAB代码:

^{pr2}$

matlab中的softmax函数:

^{3}$

以及ReLU函数:% ReLU activivation function. We will need this later.

function f = ReLU(X)

f = arrayfun(@(x) ReLU0D(x),X);

end

%ReLU activation function for a scalar

function f = ReLU0D(x)

if x < 0

f = 0;

else

f = x;

end

end

结果是,Matlab代码的行为与它应该的一样(即res和为1),但它只是在大多数时候得到错误的结果。但准确度应该在95%左右(用tensorflow测量)。我做错什么了?在

编辑:在Matlab中可视化图片效果很好。在

编辑:我正在添加我写的代码来制作.csv图片import gzip

import numpy as np

f = gzip.open('train-images-idx3-ubyte.gz','r')

image_size = 28

num_images = 20

f.read(16)

buf = f.read(image_size * image_size * num_images)

data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)

data = data.reshape(num_images, image_size, image_size, 1)

for i in range(num_images):

string = 'img'

string +=str(i)

string +=".csv"

image = np.asarray(data[i]).squeeze()

image= np.int32(image)

np.savetxt(string, image, delimiter=",")

 类似资料: