日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)

發(fā)布時(shí)間:2025/7/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#libsvm格式 數(shù)據(jù) write libsvm

? ?

#!/usr/bin/env python

#coding=gbk

# ==============================================================================

# \file gen-records.py

# \author chenghuige

# \date 2016-08-12 11:52:01.952044

# \Description

# ==============================================================================

? ?

?

from __future__ import absolute_import

from __future__ import division

#from __future__ import print_function

? ?

import sys,os

? ?

import tensorflow as tf

import numpy as np

? ?

flags = tf.app.flags

FLAGS = flags.FLAGS

? ?

_float_feature = lambda v: tf.train.Feature(float_list=tf.train.FloatList(value=v))

? ?

_int_feature = lambda v: tf.train.Feature(int64_list=tf.train.Int64List(value=v))

? ?

#how to store global info, using sequence example?

def main(argv):

writer = tf.python_io.TFRecordWriter(argv[2])

for line in open(argv[1]):

l = line.rstrip().split()

label = int(l[0])

?

start = 1

num_features = 0

if ':' not in l[1]:

num_features = int(l[1])

start += 1

?

indexes = []

values = []

?

for item in l[start:]:

index,value = item.split(':')

indexes.append(int(index))

values.append(float(value))

?

example = tf.train.Example(features=tf.train.Features(feature={

'label': _int_feature([label]),

'num_features': _int_feature

'index': _int_feature(indexes),

'value': _float_feature(values)

}))

writer.write(example.SerializeToString())

? ?

if __name__ == '__main__':

tf.app.run()

? ?

? ?

#libsvm格式 數(shù)據(jù) read libsvm

? ?

#!/usr/bin/env python

#coding=gbk

# ==============================================================================

# \file read-records.py

# \author chenghuige

# \date 2016-07-19 17:09:07.466651

# \Description

# ==============================================================================

? ?

#@TODO treat comment as sparse input ?

?

from __future__ import absolute_import

from __future__ import division

#from __future__ import print_function

? ?

import sys, os, time

import tensorflow as tf

? ?

import numpy as np

? ?

flags = tf.app.flags

FLAGS = flags.FLAGS

? ?

flags.DEFINE_integer('batch_size', 5, 'Batch size.')

flags.DEFINE_integer('num_epochs', 10, 'Number of epochs to run trainer.')

flags.DEFINE_integer('num_preprocess_threads', 12, '')

? ?

MIN_AFTER_DEQUEUE = 10000

? ?

def read(filename_queue):

reader = tf.TFRecordReader()

_, serialized_example = reader.read(filename_queue)

return serialized_example

? ?

def decode(batch_serialized_examples):

features = tf.parse_example(

batch_serialized_examples,

features={

'label' : tf.FixedLenFeature([], tf.int64),

'index' : tf.VarLenFeature(tf.int64),

'value' : tf.VarLenFeature(tf.float32),

})

? ?

label = features['label']

index = features['index']

value = features['value']

? ?

return label, index, value

? ?

def batch_inputs(files, batch_size, num_epochs = None, num_preprocess_threads=1):

"""Reads input data num_epochs times.

"""

if not num_epochs: num_epochs = None

? ?

with tf.name_scope('input'):

filename_queue = tf.train.string_input_producer(

files, num_epochs=num_epochs)

? ?

serialized_example = read(filename_queue)

batch_serialized_examples = tf.train.shuffle_batch(

[serialized_example],

batch_size=batch_size,

num_threads=num_preprocess_threads,

capacity=MIN_AFTER_DEQUEUE + (num_preprocess_threads + 1) * batch_size,

# Ensures a minimum amount of shuffling of examples.

min_after_dequeue=MIN_AFTER_DEQUEUE)

? ?

return decode(batch_serialized_examples)

? ?

def read_records():

# Tell TensorFlow that the model will be built into the default Graph.

with tf.Graph().as_default():

# Input images and labels.

tf_record_pattern = sys.argv[1]

data_files = tf.gfile.Glob(tf_record_pattern)

label, index, value = batch_inputs(data_files,

batch_size=FLAGS.batch_size,

num_epochs=FLAGS.num_epochs,

num_preprocess_threads=FLAGS.num_preprocess_threads)

? ?

# The op for initializing the variables.

init_op = tf.group(tf.initialize_all_variables(),

tf.initialize_local_variables())

? ?

# Create a session for running operations in the Graph.

#sess = tf.Session()

sess = tf.InteractiveSession()

#init_op = tf.initialize_all_variables()

#self.session.run(init)

? ?

# Initialize the variables (the trained variables and the

# epoch counter).

sess.run(init_op)

? ?

# Start input enqueue threads.

coord = tf.train.Coordinator()

threads = tf.train.start_queue_runners(sess=sess, coord=coord)

? ?

try:

step = 0

while not coord.should_stop():

start_time = time.time()

label_, index_, value_ = sess.run([label, index, value])

print label_

print index_

print value_

print index_[0]

print index_[1]

print index_[2]

duration = time.time() - start_time

step += 1

except tf.errors.OutOfRangeError:

print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))

finally:

# When done, ask the threads to stop.

coord.request_stop()

? ?

# Wait for threads to finish.

coord.join(threads)

sess.close()

? ?

? ?

def main(_):

read_records()

? ?

? ?

if __name__ == '__main__':

tf.app.run()

? ?

#文本分類 text classification

https://github.com/chenghuige/tensorflow-example

? ?

using TfRecord only need small modification, like below, I will update the code in github soon.

? ?

class SparseClassificationTrainer(object):

"""General framework for Sparse BinaryClassificationTrainer

? ?

Sparse BinaryClassfiction will use sparse embedding look up trick

see https://github.com/tensorflow/tensorflow/issues/342

"""

def __init__(self, dataset = None, num_features = 0):

if dataset is not None and type(dataset) != TfDataSet:

self.labels = dataset.labels

self.features = dataset.features

self.num_features = dataset.num_features

self.num_classes = dataset.num_classes

else:

self.features = SparseFeatures()

self.num_features = num_features

self.num_classes = None

? ?

self.index_only = False

self.total_features = self.num_features

? ?

if type(dataset) != TfDataSet:

self.sp_indices = tf.placeholder(tf.int64, name = 'sp_indices')

self.sp_shape = tf.placeholder(tf.int64, name = 'sp_shape')

self.sp_ids_val = tf.placeholder(tf.int64, name = 'sp_ids_val')

self.sp_weights_val = tf.placeholder(tf.float32, name = 'sp_weights_val')

self.sp_ids = tf.SparseTensor(self.sp_indices, self.sp_ids_val, self.sp_shape)

self.sp_weights = tf.SparseTensor(self.sp_indices, self.sp_weights_val, self.sp_shape)

? ?

self.X = (self.sp_ids, self.sp_weights)

self.Y = tf.placeholder(tf.int32) #same as batch size

else:

self.X = (dataset.index, dataset.value)

self.Y = dataset.label

?

self.type = 'sparse'

? ?

? ?

? ?

MIN_AFTER_DEQUEUE = 10000

def read(filename_queue):

reader = tf.TFRecordReader()

_, serialized_example = reader.read(filename_queue)

return serialized_example

? ?

def decode(batch_serialized_examples):

features = tf.parse_example(

batch_serialized_examples,

features={

'label' : tf.FixedLenFeature([], tf.int64),

'index' : tf.VarLenFeature(tf.int64),

'value' : tf.VarLenFeature(tf.float32),

})

? ?

label = features['label']

index = features['index']

value = features['value']

? ?

return label, index, value

? ?

def batch_inputs(files, batch_size, num_epochs=None, num_preprocess_threads=12):

if not num_epochs: num_epochs = None

? ?

with tf.name_scope('input'):

filename_queue = tf.train.string_input_producer(

files, num_epochs=num_epochs)

? ?

serialized_example = read(filename_queue)

batch_serialized_examples = tf.train.shuffle_batch(

[serialized_example],

batch_size=batch_size,

num_threads=num_preprocess_threads,

capacity=MIN_AFTER_DEQUEUE + (num_preprocess_threads + 1) * batch_size,

# Ensures a minimum amount of shuffling of examples.

min_after_dequeue=MIN_AFTER_DEQUEUE)

? ?

return decode(batch_serialized_examples

class TfDataSet(object):

def __init__(self, data_files):

self.data_files = data_files

#@TODO now only deal sparse input

self.features = SparseFeatures()

self.label = None

? ?

def build_read_graph(self, batch_size):

tf_record_pattern = self.data_files

data_files = tf.gfile.Glob(tf_record_pattern)

self.label, self.index, self.value = batch_inputs(data_files, batch_size)

? ?

? ?

? ?

def next_batch(self, sess):

label, index, value = sess.run([self.label, self.index, self.value])

? ?

trX = (index, value)

trY = label

? ?

return trX, trY

? ?

? ?

? ?

trainset = melt.load_dataset(trainset_file, is_record=FLAGS.is_record)

if FLAGS.is_record:

trainset.build_read_graph(batch_size)

?

step = 0

while not coord.should_stop():

#self.trainer.X, self.trainer.Y = trainset.next_batch(self.session)

_, cost_, accuracy_ = self.session.run([self.train_op, self.cost, self.accuracy])

if step % 100 == 0:

print 'step:', step, 'train precision@1:', accuracy_,'cost:', cost_

if step % 1000 == 0:

pass

step +=

總結(jié)

以上是生活随笔為你收集整理的Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美一区二区日韩一区二区 | 亚洲AV无码精品一区二区三区 | 美国美女黄色片 | 999精品在线视频 | 喷水在线观看 | 日本在线不卡一区 | 91漂亮少妇露脸在线播放 | 国产www在线 | 伊人网站在线观看 | va婷婷 | 亚洲午夜久久久久久久国产 | 久久精品欧美日韩精品 | 欧美草逼视频 | 成年人在线视频免费观看 | a级片在线播放 | 欧美日韩在线观看一区 | 日韩在线精品视频 | 尤物193.com| 色狠狠一区二区三区香蕉 | 天天看夜夜看 | 久久精品视频16 | 国产人妖一区 | 亚洲中文字幕在线观看 | 亚洲不卡免费视频 | 亚洲av男人的天堂在线观看 | 夜夜夜夜爽| 国产欧美一区二区三区免费看 | av中文字幕第一页 | 亚洲顶级毛片 | 美女黄色片网站 | 欧产日产国产精品 | 好吊操精品视频 | 日本黄色xxxx| 成人性生生活性生交全黄 | 亚洲国产欧美自拍 | 椎名空在线观看 | 卡一卡二视频 | 中文字幕观看在线 | 看欧美一级片 | v8888av| 好屌妞视频这里只有精品 | 日本韩国欧美在线 | 97国产精品视频 | 亚洲最大av | 在线免费播放av | 久久高清av | 亚洲私拍 | 久久精品国产熟女亚洲AV麻豆 | 午夜影视免费 | 欧美三p| 性歌舞团一区二区三区视频 | 91老肥熟| 国产免费叼嘿网站免费 | 永久免费成人代码 | 亚洲自拍色图 | 国产一级免费 | 色婷婷激情av | 国产人妻精品一区二区三 | 国产精品久久av | 日本寂寞少妇 | av2014天堂 | 日韩欧美亚洲天堂 | 国产精品自产拍 | 国产一区二区综合 | 亚洲午夜福利在线观看 | 二区三区在线观看 | 免费在线色 | 爱爱高潮视频 | 操色网 | 国产又爽又色 | 中文字幕在线日本 | 久草精品视频 | 色婷婷激情五月 | 精品无码国产污污污免费网站 | 亚洲欧美激情另类 | 97视频一区 | 精品国产乱码一区二区三区99 | 无套内谢少妇毛片 | 善良的女朋友在线观看 | 影音先锋男人站 | 久久极品 | 15—16女人毛片 | 欧美精品欧美极品欧美激情 | 国产免费成人在线视频 | 女人的av| 天天操天天干天天插 | 豆花视频在线播放 | 91精品久久久久久粉嫩 | www.色婷婷 | 西西人体做爰大胆gogo直播 | 色啪网站| 最新中文字幕在线视频 | 久久综合久久久久 | 国产伊人av | 天天综合人人 | 四虎影视8848hh | 911精品| 男人天堂视频网 | 亚洲天堂av线|