library_name:setfittags:-setfit-sentence-transformers-text-classification-generated_from_setfit_trainermetrics:-accuracy-precision-recall-f1widget:-text:> <p><a href="https://kwotsin.github.io/tech/2017/02/11/transfer-learning.html" rel="nofollow noreferrer">https://kwotsin.github.io/tech/2017/02/11/transfer-learning.html</a>Ifollowedtheabovelinktomakeaimageclassifier</p><p>Trainingcode:</p><pre><code>slim=tf.contrib.slimdataset_dir='./data'log_dir='./log'checkpoint_file='./inception_resnet_v2_2016_08_30.ckpt'image_size=299num_classes=21vlabels_file='./labels.txt'labels=open(labels_file,'r')labels_to_name= {}
for line in labels:label,string_name=line.split(':')string_name=string_name[:-1]labels_to_name[int(label)]=string_namefile_pattern='test_%s_*.tfrecord'items_to_descriptions= {
'image':'A 3-channel RGB coloured product image',
'label':'A label that from 20 labels'
}
num_epochs=10batch_size=16initial_learning_rate=0.001learning_rate_decay_factor=0.7num_epochs_before_decay=4defget_split(split_name,dataset_dir,file_pattern=file_pattern,file_pattern_for_counting='products'):ifsplit_namenotin ['train', 'validation']:raiseValueError('The split_name %s is not recognized. Please input either train or validation as the split_name'%(split_name))file_pattern_path=os.path.join(dataset_dir,file_pattern%(split_name))num_samples=0file_pattern_for_counting=file_pattern_for_counting+'_'+split_nametfrecords_to_count= [os.path.join(dataset_dir, file)forfileinos.listdir(dataset_dir)iffile.startswith(file_pattern_for_counting)]
for tfrecord_file in tfrecords_to_count:forrecordintf.python_io.tf_record_iterator(tfrecord_file):num_samples+=1test=num_samplesreader=tf.TFRecordReaderkeys_to_features= {
'image/encoded':tf.FixedLenFeature((), tf.string, default_value=''),
'image/format':tf.FixedLenFeature((), tf.string, default_value='jpg'),
'image/class/label':tf.FixedLenFeature(
[], tf.int64, default_value=tf.zeros([], dtype=tf.int64)),
}
items_to_handlers= {
'image':slim.tfexample_decoder.Image(),
'label':slim.tfexample_decoder.Tensor('image/class/label'),
}
decoder=slim.tfexample_decoder.TFExampleDecoder(keys_to_features,items_to_handlers)labels_to_name_dict=labels_to_namedataset=slim.dataset.Dataset(data_sources=file_pattern_path,decoder=decoder,reader=reader,num_readers=4,num_samples=num_samples,num_classes=num_classes,labels_to_name=labels_to_name_dict,items_to_descriptions=items_to_descriptions)returndatasetdefload_batch(dataset,batch_size,height=image_size,width=image_size,is_training=True):''' Loads a batch for training. INPUTS: - dataset(Dataset): a Dataset class object that is created from the get_split function - batch_size(int): determines how big of a batch to train - height(int): the height of the image to resize to during preprocessing - width(int): the width of the image to resize to during preprocessing - is_training(bool): to determine whether to perform a training or evaluation preprocessing OUTPUTS: - images(Tensor): a Tensor of the shape (batch_size, height, width, channels) that contain one batch of images - labels(Tensor): the batch'slabelswiththeshape(batch_size,)(requiresone_hot_encoding).''' # First create the data_provider object data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=24 + 3 * batch_size, common_queue_min=24) # Obtain the raw image using the get method raw_image, label = data_provider.get(['image','label'])# Perform the correct preprocessing for this image depending if it is training or evaluatingimage=inception_preprocessing.preprocess_image(raw_image,height,width,is_training)# As for the raw images, we just do a simple reshape to batch it upraw_image=tf.expand_dims(raw_image,0)raw_image=tf.image.resize_nearest_neighbor(raw_image, [height, width])raw_image=tf.squeeze(raw_image)# Batch up the image by enqueing the tensors internally in a FIFO queue and dequeueing many elements with tf.train.batch.images,raw_images,labels=tf.train.batch(
[image, raw_image, label],batch_size=batch_size,num_threads=4,capacity=4*batch_size,allow_smaller_final_batch=True)returnimages,raw_images,labelsdefrun():# Create the log directory here. Must be done here otherwise import will activate this unneededly.ifnotos.path.exists(log_dir):os.mkdir(log_dir)# ======================= TRAINING PROCESS =========================# Now we start to construct the graph and build our modelwithtf.Graph().as_default()as graph:tf.logging.set_verbosity(tf.logging.INFO)# Set the verbosity to INFO level# First create the dataset and load one batchdataset=get_split('train',dataset_dir,file_pattern=file_pattern)images,_,labels=load_batch(dataset,batch_size=batch_size)# Know the number steps to take before decaying the learning rate and batches per epochnum_batches_per_epoch=int(dataset.num_samples/batch_size)num_steps_per_epoch=num_batches_per_epoch# Because one step is one batch processeddecay_steps=int(num_epochs_before_decay*num_steps_per_epoch)# Create the model inferencewithslim.arg_scope(inception_resnet_v2_arg_scope()):logits,end_points=inception_resnet_v2(images,num_classes=dataset.num_classes,is_training=True)# Define the scopes that you want to exclude for restorationexclude= ['InceptionResnetV2/Logits', 'InceptionResnetV2/AuxLogits']
variables_to_restore=slim.get_variables_to_restore(exclude=exclude)# Perform one-hot-encoding of the labels (Try one-hot-encoding within the load_batch function!)one_hot_labels=slim.one_hot_encoding(labels,dataset.num_classes)# Performs the equivalent to tf.nn.sparse_softmax_cross_entropy_with_logits but enhanced with checksloss=tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,logits=logits)total_loss=tf.losses.get_total_loss()# obtain the regularization losses as well# Create the global step for monitoring the learning_rate and training.global_step=get_or_create_global_step()# Define your exponentially decaying learning ratelr=tf.train.exponential_decay(learning_rate=initial_learning_rate,global_step=global_step,decay_steps=decay_steps,decay_rate=learning_rate_decay_factor,staircase=True)# Now we can define the optimizer that takes on the learning rateoptimizer=tf.train.AdamOptimizer(learning_rate=lr)# Create the train_op.train_op=slim.learning.create_train_op(total_loss,optimizer)# State the metrics that you want to predict. We get a predictions that is not one_hot_encoded.predictions=tf.argmax(end_points['Predictions'],1)probabilities=end_points['Predictions']accuracy,accuracy_update=tf.contrib.metrics.streaming_accuracy(predictions,labels)metrics_op=tf.group(accuracy_update,probabilities)# Now finally create all the summaries you need to monitor and group them into one summary op.tf.summary.scalar('losses/Total_Loss',total_loss)tf.summary.scalar('accuracy',accuracy)tf.summary.scalar('learning_rate',lr)my_summary_op=tf.summary.merge_all()# Now we need to create a training step function that runs both the train_op, metrics_op and updates the global_step concurrently.deftrain_step(sess,train_op,global_step):''' Simply runs a session for the three arguments provided and gives a logging on the time elapsed for each global step '''# Check the time for each sess runstart_time=time.time()total_loss,global_step_count,_=sess.run([train_op,global_step,metrics_op])time_elapsed=time.time()-start_time# Run the logging to print some resultslogging.info('globalstep%s:loss:%.4f(%.2fsec/step)',global_step_count,total_loss,time_elapsed)returntotal_loss,global_step_count# Now we create a saver function that actually restores the variables from a checkpoint file in a sesssaver=tf.train.Saver(variables_to_restore)defrestore_fn(sess):returnsaver.restore(sess,checkpoint_file)# Define your supervisor for running a managed session. Do not run the summary_op automatically or else it will consume too much memorysv=tf.train.Supervisor(logdir=log_dir,summary_op=None,init_fn=restore_fn)# Run the managed sessionwithsv.managed_session()as sess:forstepinxrange(num_steps_per_epoch*num_epochs):# At the start of every epoch, show the vital information:ifstep%num_batches_per_epoch==0:logging.info('Epoch%s/%s',step/num_batches_per_epoch+1,num_epochs)learning_rate_value,accuracy_value=sess.run([lr,accuracy])logging.info('CurrentLearning Rate:%s',learning_rate_value)logging.info('CurrentStreaming Accuracy:%s',accuracy_value)# optionally, print your logits and predictions for a sanity check that things are going fine.logits_value,probabilities_value,predictions_value,labels_value=sess.run(
[logits, probabilities, predictions, labels])print'logits: \n',logits_valueprint'Probabilities: \n',probabilities_valueprint'predictions: \n',predictions_valueprint'Labels:\n:',labels_value# Log the summaries every 10 step.ifstep%10==0:loss,_=train_step(sess,train_op,sv.global_step)summaries=sess.run(my_summary_op)sv.summary_computed(sess,summaries)# If not, simply run the training stepelse:loss,_=train_step(sess,train_op,sv.global_step)# We log the final training loss and accuracylogging.info('FinalLoss:%s',loss)logging.info('FinalAccuracy:%s',sess.run(accuracy))# Once all the training has been done, save the log files and checkpoint modellogging.info('Finishedtraining!Savingmodeltodisknow.')sv.saver.save(sess,sv.save_path,global_step=sv.global_step)</code></pre><p>ThiscodeseemstoworkanIhaverantrainingonsomesampledataandImgetting94%accuracy</p><p>Evaluationcode:</p><pre><code>log_dir='./log'log_eval='./log_eval_test'dataset_dir='./data'batch_size=10num_epochs=1checkpoint_file=tf.train.latest_checkpoint('./')defrun():ifnotos.path.exists(log_eval):os.mkdir(log_eval)withtf.Graph().as_default()as graph:tf.logging.set_verbosity(tf.logging.INFO)dataset=get_split('train',dataset_dir)images,raw_images,labels=load_batch(dataset,batch_size=batch_size,is_training=False)num_batches_per_epoch=dataset.num_samples/batch_sizenum_steps_per_epoch=num_batches_per_epochwithslim.arg_scope(inception_resnet_v2_arg_scope()):logits,end_points=inception_resnet_v2(images,num_classes=dataset.num_classes,is_training=False)variables_to_restore=slim.get_variables_to_restore()saver=tf.train.Saver(variables_to_restore)defrestore_fn(sess):returnsaver.restore(sess,checkpoint_file)predictions=tf.argmax(end_points['Predictions'],1)accuracy,accuracy_update=tf.contrib.metrics.streaming_accuracy(predictions,labels)metrics_op=tf.group(accuracy_update)global_step=get_or_create_global_step()global_step_op=tf.assign(global_step,global_step+1)defeval_step(sess,metrics_op,global_step):''' Simply takes in a session, runs the metrics op and some logging information. '''start_time=time.time()_,global_step_count,accuracy_value=sess.run([metrics_op,global_step_op,accuracy])time_elapsed=time.time()-start_timelogging.info('GlobalStep%s:Streaming Accuracy:%.4f(%.2fsec/step)',global_step_count,accuracy_value,time_elapsed)returnaccuracy_valuetf.summary.scalar('Validation_Accuracy',accuracy)my_summary_op=tf.summary.merge_all()sv=tf.train.Supervisor(logdir=log_eval,summary_op=None,saver=None,init_fn=restore_fn)withsv.managed_session()as sess:forstepinxrange(num_steps_per_epoch*num_epochs):sess.run(sv.global_step)ifstep%num_batches_per_epoch==0:logging.info('Epoch:%s/%s',step/num_batches_per_epoch+1,num_epochs)logging.info('CurrentStreaming Accuracy:%.4f',sess.run(accuracy))ifstep%10==0:eval_step(sess,metrics_op=metrics_op,global_step=sv.global_step)summaries=sess.run(my_summary_op)sv.summary_computed(sess,summaries)else:eval_step(sess,metrics_op=metrics_op,global_step=sv.global_step)logging.info('FinalStreaming Accuracy:%.4f',sess.run(accuracy))raw_images,labels,predictions=sess.run([raw_images,labels,predictions])foriinrange(10):image,label,prediction=raw_images[i],labels[i],predictions[i]prediction_name,label_name=dataset.labels_to_name[prediction],dataset.labels_to_name[label]text='Prediction: %s \n Ground Truth: %s'%(prediction_name,label_name)img_plot=plt.imshow(image)plt.title(text)img_plot.axes.get_yaxis().set_ticks([])img_plot.axes.get_xaxis().set_ticks([])plt.show()logging.info('Model evaluation has completed! Visit TensorBoard for more information regarding your evaluation.')</code></pre><p>Soaftertrainingthemodelandgetting94%accuracyitriedtoevaluatethemodel.OnevaluationIget0-1%accuracythewholetime.Iinvestigatedthisonlytofindthatitispredictingthesameclasseverytime</p><pre><code>labels: [7, 11, 5, 1, 20, 0, 18, 1, 0, 7]
predictions: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
</code></pre><p>Cananyonehelpinwhereimaybegoingwrong?</p><p>EDIT:</p><p>TensorBoardaccuracyandlossformtraining</p><p><ahref="https://i.stack.imgur.com/NLiwC.png"rel="nofollownoreferrer"><imgsrc="https://i.stack.imgur.com/NLiwC.png"alt="enterimagedescriptionhere"></a><ahref="https://i.stack.imgur.com/QdX6d.png"rel="nofollownoreferrer"><imgsrc="https://i.stack.imgur.com/QdX6d.png"alt="enterimagedescriptionhere"></a></p><p>TensorBoardaccuracyfromevaluation</p><p><ahref="https://i.stack.imgur.com/TNE5B.png"rel="nofollownoreferrer"><imgsrc="https://i.stack.imgur.com/TNE5B.png"alt="enterimagedescriptionhere"></a></p><p>EDIT:</p><p>Ivestillnotbeenabletosolvethisissues.IthoughttheremightbeaproblemwithhowIamrestoringthegraphintheevalscriptsoItriedusingthistorestorethemodelinstead</p><pre><code>saver=tf.train.import_meta_graph('/log/model.ckpt.meta')defrestore_fn(sess):returnsaver.restore(sess,checkpoint_file)</code></pre><p>insteadof</p><pre><code>variables_to_restore=slim.get_variables_to_restore()saver=tf.train.Saver(variables_to_restore)defrestore_fn(sess):returnsaver.restore(sess,checkpoint_file)</code></pre><p>andjustjusttakesaverylongtimetostartandfinallyerrors.IthentriedusingV1ofthewriterinthesaver(<code>saver=tf.train.Saver(variables_to_restore,write_version=saver_pb2.SaveDef.V1)</code>)andretrainedandwasunabletoloadthischeckpointatallasitsaidvariableswasmissing.</p><p>IalsoattemptedtorunmyevalscriptwiththesamedataittrainedonjusttoseeifthismaygivedifferentresultsyetIgetthesame.</p><p>FinallyIre-clonedtherepofromtheurlandranatrainusingthesamedatasetinthetutorialandIget0-3%accuracywhenIevaluateevenaftergettingitto84%whilsttraining.AlsomycheckpointsmusthavethecorrectinformationaswhenIrestarttrainingtheaccuracycontinuesfromwhereitleftof.Itfeelslikei'mnotdoingsomethingcorrectlywhenIrestorethemodel.Wouldreallyappreciateanysuggestionsonthisasimatadeadendcurrently:(</p>-text:> <p>I've just started using tensorflow for a project I'm working on. The program aims to be a binary classifier with input being 12 features. The output is either normal patient or patient with a disease. The prevalence of the disease is quite low and so my dataset is very imbalanced, with 502 examples of normal controls and only 38 diseased patients. For this reason, I'm trying to use <code>tf.nn.weighted_cross_entropy_with_logits</code> as my cost function.</p><p>Thecodeisbasedontheiriscustomestimatorfromtheofficialtensorflowdocumentation,andworkswith<code>tf.losses.sparse_softmax_cross_entropy</code>asthecostfunction.However,whenIchangeto<code>weighted_cross_entropy_with_logits</code>,IgetashapeerrorandI'mnotsurehowtofixthis.</p><pre><code>ValueError:logitsandtargetsmusthavethesameshape((?,2)vs(?,))</code></pre><p>Ihavesearchedandsimilarproblemshavebeensolvedbyjustreshapingthelabels-Ihavetriedtodothisunsuccessfully(anddon'tunderstandwhy<code>tf.losses.sparse_softmax_cross_entropy</code>worksfineandtheweightedversiondoesnot).</p><p>Myfullcodeishere<ahref="https://gist.github.com/revacious/83142573700c17b8d26a4a1b84b0dff7"rel="nofollownoreferrer">https://gist.github.com/revacious/83142573700c17b8d26a4a1b84b0dff7</a></p><p>Thanks!</p>-text:> <p>In the documentation it seems they focus on how to save and restore tf.keras.models, but i was wondering how do you save and restore models trained customly through some basic iteration loop?</p><p>Nowthatthereisntagraphorasession,howdowesavestructuredefinedinatffunctionthatiscustomlybuiltwithoutusinglayerabstractions?</p>-text:> <p>I simply have <code>train = optimizer.minimize(loss = tf.constant(4,dtype="float32"))</code> Line of code that i change before everything is working. <br/></p><p>Whyitisgivingerror?Becausedocumentationsayitcanbetensor<ahref="https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Adam#minimize"rel="nofollownoreferrer">HereisDocs</a></p><pre><code>W=tf.Variable([0.5],tf.float32)b=tf.Variable([0.1],tf.float32)x=tf.placeholder(tf.float32)y=tf.placeholder(tf.float32)discounted_reward=tf.placeholder(tf.float32,shape=[4,],name="discounted_reward")linear_model=W*x+bsquared_delta=tf.square(linear_model-y)print(squared_delta)loss=tf.reduce_sum(squared_delta*discounted_reward)print(loss)optimizer=tf.train.GradientDescentOptimizer(0.01)train=optimizer.minimize(loss=tf.constant(4,dtype="float32"))init=tf.global_variables_initializer()sess=tf.Session()sess.run(init)foriinrange(3):sess.run(train,{x:[1,2,3,4],y:[0,-1,-2,-3],discounted_reward:[1,2,3,4]})print(sess.run([W,b]))</code></pre><hr><p>Ireallyneedthisthingtowork.Inthisparticularexamplewecanhaveotherwaystosolveitbutineedittoworkasmyactualcodecandothisonly</p><p><hr/>Erroris</p><pre><code>>ValueError:Nogradientsprovidedforanyvariable,checkyourgraph>foropsthatdonotsupportgradients,betweenvariables> ["<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_2:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_3:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_4:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_5:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_6:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_7:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_8:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_9:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_10:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_11:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_12:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_13:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_14:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_15:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_16:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_17:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_18:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_19:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_20:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_21:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_22:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_23:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_24:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_25:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_26:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_27:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_28:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_29:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_30:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_31:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_32:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_33:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_34:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_35:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_36:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_37:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_38:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_39:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_40:0' shape=(1,) dtype=float32_ref>",
>"<tf.Variable 'Variable_41:0' shape=(1,) dtype=float32_ref>"]
andloss>Tensor("Const_4:0",shape=(),dtype=float32).</code></pre>-text:> <p>I found in the <a href="https://www.tensorflow.org/tutorials/recurrent" rel="nofollow noreferrer">tensorflow doc</a>:</p><p><code>stacked_lstm=tf.contrib.rnn.MultiRNNCell([lstm]*number_of_layers,...</code></p><p>IneedtouseMultiRNNCell</p><p>but,Iwritethoselines</p><p><code>a= [tf.nn.rnn_cell.BasicLSTMCell(10)]*3printid(a[0]),id(a[1])</code></p><p>Itsoutputis<code>[46480636964648063696]</code>.</p><p>Can<code>MultiRNNCell</code>usethesameobject<code>BasicLSTMCell</code>asalistforparameter?</p>pipeline_tag:text-classificationinference:truebase_model:sentence-transformers/all-mpnet-base-v2model-index:-name:SetFitwithsentence-transformers/all-mpnet-base-v2results:-task:type:text-classificationname:TextClassificationdataset:name:Unknowntype:unknownsplit:testmetrics:-type:accuracyvalue:0.85name:Accuracy-type:precisionvalue:0.8535353535353536name:Precision-type:recallvalue:0.85name:Recall-type:f1value:0.8496240601503761name:F1
SetFit with sentence-transformers/all-mpnet-base-v2
I'm looking to use Tensorflow to train a neural network model for classification, and I want to read data from a CSV file, such as the Iris data set.
\n\n
The Tensorflow documentation shows an example of loading the Iris data and building a prediction model, but the example uses the high-level tf.contrib.learn API. I want to use the low-level Tensorflow API and run gradient descent myself. How would I do that?
\n'
'
In the following code, I want dense matrix B to left multiply a sparse matrix A, but I got errors.
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> \nto Tensor. Contents: SparseTensor(indices=Tensor("Placeholder_4:0", shape=(?, ?), dtype=int64), values=Tensor("Placeholder_3:0", shape=(?,), dtype=float32), dense_shape=Tensor("Placeholder_2:0", shape=(?,), dtype=int64)). \nConsider casting elements to a supported type.\n
\n\n
What's wrong with my code?
\n\n
I'm doing this following the documentation and it says we should use a_is_sparse to denote whether the first matrix is sparse, and similarly with b_is_sparse. Why is my code wrong?
\n\n
As is suggested by vijay, I should use C = tf.matmul(B,tf.sparse_tensor_to_dense(A),a_is_sparse=False,b_is_sparse=True)
\n\n
I tried this but I met with another error saying:
\n\n
Caused by op u'SparseToDense', defined at:\n File "a.py", line 19, in <module>\n C = tf.matmul(B,tf.sparse_tensor_to_dense(A),a_is_sparse=False,b_is_sparse=True)\n File "/home/fengchao.pfc/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/sparse_ops.py", line 845, in sparse_tensor_to_dense\n name=name)\n File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/sparse_ops.py", line 710, in sparse_to_dense\n name=name)\n File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_sparse_ops.py", line 1094, in _sparse_to_dense\n validate_indices=validate_indices, name=name)\n File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op\n op_def=op_def)\n File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op\n original_op=self._default_original_op, op_def=op_def)\n File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__\n self._traceback = _extract_stack()\n\nInvalidArgumentError (see above for traceback): indices[1] = [1,2] is out of order\n[[Node: SparseToDense = SparseToDense[T=DT_FLOAT, Tindices=DT_INT64, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_Placeholder_4_0_2, _arg_Placeholder_2_0_0, _arg_Placeholder_3_0_1, SparseToDense/default_value)]]\n
\n\n
Thank you all for helping me!
\n'
"
I am using tf.estimator.train_and_evaluate and tf.data.Dataset to feed data to the estimator:
The training goes fine, but when it comes to evaluation I get this error:
\n\n
OutOfRangeError (see above for traceback): End of sequence \n
\n\n
If I don't use Dataset.batch on evaluation dataset (by omitting the line dataset[name] = dataset[name].batch(batch_size) in data_fn) I get the same error but after a much longer time.
\n\n
I can only avoid this error if I don't batch the data and use steps=1 for evaluation, but does that perform the evaluation on the whole dataset?
\n\n
I don't understand what causes this error as the documentation suggests I should be able to evaluate on batches too.
\n\n
Note: I get the same error when using tf.estimator.evaluate on data batches.
\n"
0
'
I'm working on a project where I have trained a series of binary classifiers with Keras, with Tensorflow as the backend engine. The input data I have is a series of images, where each binary classifier must make the prediction on the images, later I save the predictions on a CSV file.
\n
The problem I have is when I get the predictions from the first series of binary classifiers there isn't any warning, but when the 5th or 6th binary classifier calls the method predict on the input data I get the following warning:
\n
\n
WARNING:tensorflow:5 out of the last 5 calls to <function\nModel.make_predict_function..predict_function at\n0x2b280ff5c158> triggered tf.function retracing. Tracing is expensive\nand the excessive number of tracings could be due to (1) creating\n@tf.function repeatedly in a loop, (2) passing tensors with different\nshapes, (3) passing Python objects instead of tensors. For (1), please\ndefine your @tf.function outside of the loop. For (2), @tf.function\nhas experimental_relax_shapes=True option that relaxes argument shapes\nthat can avoid unnecessary retracing. For (3), please refer to\nhttps://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args\nand https://www.tensorflow.org/api_docs/python/tf/function for more\ndetails.
\n
\n
To answer each point in the parenthesis, here are my answers:
\n\n
The predict method is called inside a for loop.
\n
I don't pass tensors but a list of NumPy arrays of gray scale images, all of them with the same size in width and height. The only thing that can change is the batch size because the list can have only 1 image or more than one.
\n
As I wrote in point 2, I pass a list of NumPy arrays.
\n\n
I have debugged my program and found that this warning always happens when the method predict is called. To summarize the code I have written is the following:
\n
import cv2 as cv\nimport tensorflow as tf\nfrom tensorflow.keras.models import load_model\n# Load the models\nbinary_classifiers = [load_model(path) for path in path2models]\n# Get the images\nimages = [#Load the images with OpenCV]\n# Apply the resizing and reshapes on the images.\nmy_list = list()\nfor image in images:\n image_reworked = # Apply the resizing and reshaping on images\n my_list.append(image_reworked)\n\n# Get the prediction from each model\n# This is where I get the warning\npredictions = [model.predict(x=my_list,verbose=0) for model in binary_classifiers]\n
\n
What I have tried
\n
I have defined a function as tf.function and putted the code of the predictions inside the tf.function like this
\n
@tf.function\ndef testing(models, faces):\n return [model.predict(x=faces,verbose=0) for model in models]\n
\n
But I ended up getting the following error:
\n
\n
RuntimeError: Detected a call to Model.predict inside a\ntf.function. Model.predict is a high-level endpoint that manages\nits own tf.function. Please move the call to Model.predict outside\nof all enclosing tf.functions. Note that you can call a Model\ndirectly on Tensors inside a tf.function like: model(x).
\n
\n
So calling the method predict is basically already a tf.function. So it's useless to define a tf.function when the warning I get it's from that method.
But neither of the two questions answers my question about how to avoid this warning. Plus I have also checked the links in the warning message but I couldn't solve my problem.
\n
What I want
\n
I simply want to avoid this warning. While I'm still getting the predictions from the models I noticed that the python program takes way too much time on doing predictions for a list of images.
\n
What I'm using
\n
\n
Python 3.6.13
\n
Tensorflow 2.3.0
\n
\n
Solution
\n
After some tries to suppress the warning from the predict method, I have checked the documentation of Tensorflow and in one of the first tutorials on how to use Tensorflow it is explained that, by default, Tensorflow is executed in eager mode, which is useful for testing and debugging the network models. Since I have already tested my models many times, it was only required to disable the eager mode by writing this single python line of code:
\n
tf.compat.v1.disable_eager_execution()
\n
Now the warning doesn't show up anymore.
\n'
'
I try to export a Tensorflow model but I can not find the best way to add the exogenous feature to the tf.contrib.timeseries.StructuralEnsembleRegressor.build_raw_serving_input_receiver_fn.
# this is the exogenous column \nstring_feature = tf.contrib.layers.sparse_column_with_keys(\n column_name="is_changepoint", keys=["no", "yes"])\n\none_hot_feature = tf.contrib.layers.one_hot_column(\n sparse_id_column=string_feature)\n\nestimator = tf.contrib.timeseries.StructuralEnsembleRegressor(\n periodicities=12, \n cycle_num_latent_values=3,\n num_features=1,\n exogenous_feature_columns=[one_hot_feature],\n exogenous_update_condition=\n lambda times, features: tf.equal(features["is_changepoint"], "yes"))\n\nreader = tf.contrib.timeseries.CSVReader(\n csv_file_name,\n\n column_names=(tf.contrib.timeseries.TrainEvalFeatures.TIMES,\n tf.contrib.timeseries.TrainEvalFeatures.VALUES,\n "is_changepoint"),\n\n column_dtypes=(tf.int64, tf.float32, tf.string),\n\n skip_header_lines=1)\n\ntrain_input_fn = tf.contrib.timeseries.RandomWindowInputFn(reader, batch_size=4, window_size=64)\nestimator.train(input_fn=train_input_fn, steps=train_steps)\nevaluation_input_fn = tf.contrib.timeseries.WholeDatasetInputFn(reader)\nevaluation = estimator.evaluate(input_fn=evaluation_input_fn, steps=1)\n\nexport_directory = tempfile.mkdtemp()\n\n###################################################### \n# the exogenous column must be provided to the build_raw_serving_input_receiver_fn. \n# But How ?\n######################################################\n\ninput_receiver_fn = estimator.build_raw_serving_input_receiver_fn()\n# -> error missing 'is_changepoint' key \n\n#input_receiver_fn = estimator.build_raw_serving_input_receiver_fn({'is_changepoint' : string_feature}) \n# -> cast exception\n\nexport_location = estimator.export_savedmodel(export_directory, input_receiver_fn)\n
\n\n
According to the documentation, build_raw_serving_input_receiver_fn exogenous_features parameter : A dictionary mapping feature keys to exogenous features (either Numpy arrays or Tensors). Used to determine the shapes of placeholders for these features.
\n\n
So what is the best way to transform the one_hot_column or sparse_column_with_keys to a Tensor object ?
\n'
"
I am currently working on an optical flow project and I come across a strange error.
\n\n
I have uint16 images stored in bytes in my TFrecords. When I read the TFrecords from my local machine it is giving me uint16 values, but when I deploy the same code and read it from the docker I am getting uint8 values eventhough my dtype is uint16. I mean the uint16 values are getting reduced to uint8 like 32768 --> 128.
\n\n
What is causing this error?
\n\n
My local machine has: Tensorflow 1.10.1 and python 3.6\nMy Docker Image has: Tensorflow 1.12.0 and python 3.5
\n\n
I am working on tensorflow object detection API\nWhile creating the TF records I use:
\n\n
with tf.gfile.GFile(flows, 'rb') as fid:\n flow_images = fid.read()\n
\n\n
While reading it back I am using: tf.image.decoderaw
\n\n
Dataset: KITTI FLOW 2015
\n"
Evaluation
Metrics
Label
Accuracy
Precision
Recall
F1
all
0.85
0.8535
0.85
0.8496
Uses
Direct Use for Inference
First install the SetFit library:
pip install setfit
Then you can load this model and run inference.
from setfit import SetFitModel
# Download from the 🤗 Hub
model = SetFitModel.from_pretrained("sharukat/sbert-questionclassifier")
# Run inference
preds = model("<p>In the documentation it seems they focus on how to save and restore tf.keras.models, but i was wondering how do you save and restore models trained customly through some basic iteration loop?</p><p>Now that there isnt a graph or a session, how do we save structure defined in a tf function that is customly built without using layer abstractions?</p>")
Training Details
Training Set Metrics
Training set
Min
Median
Max
Word count
15
330.0667
3755
Label
Training Sample Count
0
450
1
450
Training Hyperparameters
batch_size: (16, 2)
num_epochs: (1, 16)
max_steps: -1
sampling_strategy: unique
body_learning_rate: (2e-05, 1e-05)
head_learning_rate: 0.01
loss: CosineSimilarityLoss
distance_metric: cosine_distance
margin: 0.25
end_to_end: False
use_amp: False
warmup_proportion: 0.1
max_length: 256
seed: 42
eval_max_steps: -1
load_best_model_at_end: True
Training Results
Epoch
Step
Training Loss
Validation Loss
0.0000
1
0.2951
-
1.0
25341
0.0
0.2473
The bold row denotes the saved checkpoint.
Framework Versions
Python: 3.10.13
SetFit: 1.0.3
Sentence Transformers: 2.5.0
Transformers: 4.38.1
PyTorch: 2.1.2
Datasets: 2.17.1
Tokenizers: 0.15.2
Citation
BibTeX
@article{https://doi.org/10.48550/arxiv.2209.11055,
doi = {10.48550/ARXIV.2209.11055},
url = {https://arxiv.org/abs/2209.11055},
author = {Tunstall, Lewis and Reimers, Nils and Jo, Unso Eun Seo and Bates, Luke and Korat, Daniel and Wasserblat, Moshe and Pereg, Oren},
keywords = {Computation and Language (cs.CL), FOS: Computer and information sciences, FOS: Computer and information sciences},
title = {Efficient Few-Shot Learning Without Prompts},
publisher = {arXiv},
year = {2022},
copyright = {Creative Commons Attribution 4.0 International}
}