美文网首页
tensorflow编程

tensorflow编程

作者: 啊啊啊啊啊1231 | 来源:发表于2020-03-22 22:55 被阅读0次

load进inception-v3 model,   再把图片给输入模型

# Code modified from https://github.com/tensorflow/cleverhans/blob/master/examples/nips17_adversarial_competition/sample_defenses/base_inception_model/defense.py

import tensorflow.contrib.slim.nets as nets

#from tensorflow.contrib.slim.nets.inception import inception_v3, inception_v3_arg_scope

import numpy as np

import tensorflow as tf

slim = tf.contrib.slim

import pdb

height = 299

width = 299

channels = 3

num_classes=1001

X = tf.placeholder(tf.float32, shape=[None, height, width, channels])

y = tf.placeholder(tf.float32,shape=[None,182])

with slim.arg_scope(nets.inception.inception_v3_arg_scope()):

    logits, end_points = nets.inception.inception_v3_base(X,final_endpoint = "Mixed_7a")

    variables_to_restore=slim.get_variables_to_restore()

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    saver=tf.train.Saver(variables_to_restore)

    saver.restore(sess,"./inception_v3.ckpt")

    print("Done")

extract features utilizing inception-resnet-v2

import tensorflow as tf

slim = tf.contrib.slim

from inception_resnet_v2 import *

import pdb

from PIL import Image

import numpy as np

height = 299

width = 299

channels = 3

num_classes=1001

input_tensor = tf.placeholder(tf.float32, shape=[None, height, width, channels])

checkpoint_file = 'inception_resnet_v2_2016_08_30.ckpt'

sample_images = ['16548706_m.jpg']

#sample_images = ['dog.jpg', 'panda.jpg']

#Load the model

sess = tf.Session()

arg_scope = inception_resnet_v2_arg_scope()

with slim.arg_scope(arg_scope):

  logits, end_points = inception_resnet_v2(input_tensor, is_training=False)

saver = tf.train.Saver()

saver.restore(sess, checkpoint_file)

pdb.set_trace()

for image in sample_images:

  im = Image.open(image).resize((299,299))

  im = np.array(im)

  im = im.reshape(-1,299,299,3)

  #predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im})

  predict_values, logit_values = sess.run([end_points['Mixed_7a'], logits], feed_dict={input_tensor: im})

pytorch与tensorflow最大的差别:

pytorch通过pdb模块可以实现查看变量的值。

而tensorflow对于每个tf.Tensor必须要开sess.run(tf.variable)才能看其中的值。

recommendation framework常用的几个函数:

user_embeddings = tf.reshape(user_embeddings, [self.batch_size, 1, 1, self.dim])

user_relation_scores = tf.reduce_mean(user_embeddings * neighbor_relations, axis=-1)

user_relation_scores_normalized = tf.nn.softmax(user_relation_scores, dim=-1)

user_relation_scores_normalized = tf.expand_dims(user_relation_scores_normalized, axis=-1)

self.bias = tf.get_variable(shape=[self.dim], initializer=tf.zeros_initializer(), name='bias')

self.weights = tf.get_variable(shape=[self.dim, self.dim], initializer=tf.contrib.layers.xavier_initializer(), name='weights')

output = tf.nn.dropout(output, keep_prob=1-self.dropout)

output = tf.matmul(output, self.weights) + self.bias

output = tf.concat([self_vectors, neighbors_agg], axis=-1)

output = tf.nn.dropout(output, keep_prob=1-self.dropout)

embedded_contexts = tf.nn.embedding_lookup(self.context_embeddings, entities)

self.entity_embeddings = tf.Variable(entity_embs, dtype=np.float32, name='entity')

self.entity_embeddings = tf.layers.dense( self.entity_embeddings, units=args.entity_dim, activation=tf.nn.tanh, name='transformed_entity', kernel_regularizer=tf.contrib.layers.l2_regularizer(args.l2_weight))

self.labels = tf.placeholder( dtype=tf.float32, shape=[None], name='labels')

相关文章

  • 【TensorFlow】自学笔迹 | 开始Python编程

    演示小程序: 编程模式 命令式编程:Torch 符号式编程:Tensorflow (已优化) TensorFlow...

  • 20201017-Tensoflow-1

    Tensoflow 全部基于Tensorflow=1.14.0 Tensorflow的编程模式:声明式编程,告诉计...

  • TensorFlow入门

    TensorFlow编程框架 TensorFlow深度学习框架大致分为4层,结构如下 在使用TensorFlow做...

  • Tensorflow之旅(4)(戴德曼翻译)

    四. Tensorflow的编程接口 在讨论了Tensorflow计算模型之后,我们现在聚焦到更为实际的编程接口。...

  • TensorFlow 编程模式

    用符号式编程写tensorflow

  • tf实现mnist

    1.编程环境 安装tensorflow命令:pip install tensorflow操作系统:Win7pyth...

  • TensorFlow 高效编程

    TensorFlow 高效编程 原文:vahidk/EffectiveTensorflow 译者:FesianXu...

  • tensorflow编程

    load进inception-v3 model, 再把图片给输入模型 # Code modified from h...

  • TensorFlow基本概念

    TensorFlow是一个编程系统。 在TensorFlow中,使用图(graphs)来表示计算任务,图(grap...

  • 3. 变量概述

    TensorFlow的变量在编程中被广泛使用,用于表示共享持久的状态。本章讲解了TensorFlow创建、更新、管...

网友评论

      本文标题:tensorflow编程

      本文链接:https://www.haomeiwen.com/subject/dcvbyhtx.html