tensorflow2.0

Implement custom loss function in Tensorflow 2.0

拟墨画扇 提交于 2019-12-12 23:57:02
问题 I'm building a model for Time series classification. The data is very unbalanced so I've decided to use a weighted cross entropy function as my loss. Tensorflow provides tf.nn.weighted_cross_entropy_with_logits but I'm not sure how to use it in TF 2.0. Because my model is build using tf.keras API I was thinking about creating my custom loss function like this: pos_weight=10 def weighted_cross_entropy_with_logits(y_true,y_pred): return tf.nn.weighted_cross_entropy_with_logits(y_true,y_pred,pos

How to change a learning rate for Adam in TF2?

吃可爱长大的小学妹 提交于 2019-12-12 20:23:02
问题 How to change the learning rate of Adam optimizer, while learning is progressing in TF2? There some answers floating around, but applicable to TF1, e.g. using feed_dict. 回答1: You can read and assign the learning rate via a callback. So you can use something like this: class LearningRateReducerCb(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): old_lr = self.model.optimizer.lr.read_value() new_lr = old_lr * 0.99 print("\nEpoch: {}. Reducing Learning Rate from {} to {}"

Basic function minimisation and variable tracking in TensorFlow 2.0

Deadly 提交于 2019-12-12 19:07:02
问题 I am trying to perform the most basic function minimisation possible in TensorFlow 2.0, exactly as in the question Tensorflow 2.0: minimize a simple function, however I cannot get the solution described there to work. Here is my attempt, mostly copy-pasted but with some bits that seemed to be missing added in. import tensorflow as tf x = tf.Variable(2, name='x', trainable=True, dtype=tf.float32) with tf.GradientTape() as t: y = tf.math.square(x) # Is the tape that computes the gradients!

Optimise function for many pseudodata realisations in TensorFlow 2

那年仲夏 提交于 2019-12-12 07:20:14
问题 My end goal is to simulate likelihood ratio test statistics, however, the core problem I am having is that I do not understand how to get TensorFlow 2 to perform many optimizations for different data inputs. Here is my attempt, hopefully, it gives you the idea of what I am trying: import tensorflow as tf import tensorflow_probability as tfp from tensorflow_probability import distributions as tfd import numpy as np # Bunch of independent Poisson distributions that we want to combine poises0 =

How do I create a regression model with multiple outputs in tf.keras?

时间秒杀一切 提交于 2019-12-12 01:00:11
问题 I'm attempting to train a regression model to predict attributes of music such as BPM. The model takes in spectrograms of audio snippets that are 256x128px png files and outputs a couple continuous values. I have the following code so far that I have developed based upon this guide on the tensorflow website: import tensorflow as tf import os import random import pathlib AUTOTUNE = tf.data.experimental.AUTOTUNE TRAINING_DATA_DIR = r'specgrams' def gen_model(): model = tf.keras.models

Tensorflow 2.0 Beta: Model.fit() throws ValueError: Arguments and signature arguments do not match: 56 57

老子叫甜甜 提交于 2019-12-11 14:55:10
问题 I'm new to machine learning. I'm trying to make a simple RNN in Tensorflow 2.0 but I'm hitting a snag. I've reduced it to a minimal example that reproduces the problem. The goal of this minimal example is for the RNN to learn to output 1.0 repeatedly. import os import sys import math from random import shuffle import numpy as np import tensorflow as tf from time import time as time epochs = 200 batch_size = 32 chunk_length = 64 features = 10 def main(): train_dataset = np.zeros([batch_size,

How to cache and iterate through a Dataset of unknown size?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:38:39
问题 While adding the .cache() step to my dataset pipeline, successives training epochs still download the data from the network storage. I have a dataset on a network storage. I want to cache it, but not to repeat it: a training epoch must run through the whole dataset. Here is my dataset building pipeline: return tf.data.Dataset.list_files( file_pattern ).interleave( tf.data.TFRecordDataset, num_parallel_calls=tf.data.experimental.AUTOTUNE ).shuffle( buffer_size=2048 ).batch( batch_size=2048,

Train complicated nn models with tf.eager (better with TF2 symbolic support)

杀马特。学长 韩版系。学妹 提交于 2019-12-11 02:43:45
问题 Is there (more or less) simple way to write a complicated NN model so it will be trainable in the eager mode? Are there examples of a such code? For example, I want to use the InceptionResnetV2 . I have the code created with tf.contrib.slim . According to this link, https://github.com/tensorflow/tensorflow/issues/16182 , slim is deprecated and I need to use Keras . And I really can't use a slim code for training with eager because I can't take the list of variables and apply gradients (ok, I

How to convert “tensor” to “numpy” array in tensorflow?

徘徊边缘 提交于 2019-12-11 00:21:55
问题 I am trying to convert a tensor to numpy in the tesnorflow2.0 version. Since tf2.0 have eager execution enabled then it should work by default and working too in normal runtime. While I execute code in tf.data.Dataset API then it gives an error "AttributeError: 'Tensor' object has no attribute 'numpy'" I have tried ".numpy()" after tensorflow variable and for ".eval()" I am unable to get default session. from __future__ import absolute_import, division, print_function, unicode_literals import

How to improve data input pipeline performance?

怎甘沉沦 提交于 2019-12-10 16:31:13
问题 I try to optimize my data input pipeline. The dataset is a set of 450 TFRecord files of size ~70MB each, hosted on GCS. The job is executed with GCP ML Engine. There is no GPU. Here is the pipeline: def build_dataset(file_pattern): return tf.data.Dataset.list_files( file_pattern ).interleave( tf.data.TFRecordDataset, num_parallel_calls=tf.data.experimental.AUTOTUNE ).shuffle( buffer_size=2048 ).batch( batch_size=2048, drop_remainder=True, ).cache( ).repeat( ).map( map_func=_parse_example