Skip to content

Commit

Permalink
Enable PyPI installation
Browse files Browse the repository at this point in the history
* Fix or toggle off warnings (mostly TF deprecation warnings)
* Update dependency to TF>=1.10.0
* Upgrade to version v0.2.2
  • Loading branch information
ZhitingHu authored Aug 5, 2019
2 parents 81cb96a + 26c119f commit 361fcdc
Show file tree
Hide file tree
Showing 37 changed files with 122 additions and 80 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

### Feature improvements

### Fixes

## [v0.2.2](https://github.com/asyml/texar/releases/tag/v0.2.2) (2019-08-05)

### New features

* Enable installation from [Pypi](https://pypi.org/project/texar/). ([#186](https://github.com/asyml/texar/pull/186))

### Feature improvements

* Use lazy import to be compatible with [texar-pytorch](https://github.com/asyml/texar-pytorch). ([#183](https://github.com/asyml/texar/pull/183))

### Fixes
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

-----------------


[![pypi](https://img.shields.io/pypi/v/texar.svg)](https://pypi.python.org/pypi/texar)
[![Build Status](https://travis-ci.org/asyml/texar.svg?branch=master)](https://travis-ci.org/asyml/texar)
[![Documentation Status](https://readthedocs.org/projects/texar/badge/?version=latest)](https://texar.readthedocs.io/en/latest/?badge=latest)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/asyml/texar/blob/master/LICENSE)
Expand Down Expand Up @@ -79,6 +81,13 @@ agent = tx.agents.SeqPGAgent(samples=outputs.sample_id,
Many more examples are available [here](./examples)

### Installation
Besides the packages in requirements.txt, Texar additionally requires:

* tensorflow >= 1.10.0 (but <= 2.0). Follow the tensorflow official instructions to install the appropriate version

* tensorflow_probability >= 0.3.0. Follow the tensorflow_probability official instractions to install.

After Tensorflow is installed, please run the following commands to install Texar:
```
git clone https://github.com/asyml/texar.git
cd texar
Expand Down
4 changes: 2 additions & 2 deletions examples/bert/bert_classifier_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def main(_):
input_ids = batch["input_ids"]
segment_ids = batch["segment_ids"]
batch_size = tf.shape(input_ids)[0]
input_length = tf.reduce_sum(1 - tf.to_int32(tf.equal(input_ids, 0)),
input_length = tf.reduce_sum(1 - tf.cast(tf.equal(input_ids, 0), tf.int32),
axis=1)
# Builds BERT
with tf.variable_scope('bert'):
Expand Down Expand Up @@ -198,7 +198,7 @@ def _train_epoch(sess):
periodically.
"""
iterator.restart_dataset(sess, 'train')

fetches = {
'train_op': train_op,
'loss': loss,
Expand Down
2 changes: 1 addition & 1 deletion examples/bert/bert_classifier_main_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def main(_):
input_ids = batch["input_ids"]
segment_ids = batch["segment_ids"]
batch_size = tf.shape(input_ids)[0]
input_length = tf.reduce_sum(1 - tf.to_int32(tf.equal(input_ids, 0)),
input_length = tf.reduce_sum(1 - tf.cast(tf.equal(input_ids, 0), tf.int32),
axis=1)
# Builds BERT
hparams = {
Expand Down
13 changes: 7 additions & 6 deletions examples/seq2seq_exposure_bias/interpolation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tensorflow as tf
import numpy as np

from tensorflow_probability import distributions as tfpd
from tensorflow.contrib.seq2seq import SampleEmbeddingHelper
from texar.evals.bleu import sentence_bleu
from rouge import Rouge
Expand Down Expand Up @@ -95,19 +96,19 @@ def sample(self, time, outputs, state, name=None):
of 'state'([decoded_ids, rnn_state])
"""
sample_method_sampler = \
tf.distributions.Categorical(probs=self._lambdas)
tfpd.Categorical(probs=self._lambdas)
sample_method_id = sample_method_sampler.sample()

truth_feeding = lambda: tf.cond(
tf.less(time, tf.shape(self._ground_truth)[1]),
lambda: tf.to_int32(self._ground_truth[:, time]),
lambda: tf.cast(self._ground_truth[:, time], tf.int32),
lambda: tf.ones_like(self._ground_truth[:, 0],
dtype=tf.int32) * self._vocab.eos_token_id)

self_feeding = lambda : SampleEmbeddingHelper.sample(
self_feeding = lambda: SampleEmbeddingHelper.sample(
self, time, outputs, state, name)

reward_feeding = lambda : self._sample_by_reward(time, state)
reward_feeding = lambda: self._sample_by_reward(time, state)

sample_ids = tf.cond(
tf.logical_or(tf.equal(time, 0), tf.equal(sample_method_id, 1)),
Expand Down Expand Up @@ -207,9 +208,9 @@ def _get_rewards(time, prefix_ids, target_ids, ground_truth_length):

return result

sampler = tf.distributions.Categorical(
sampler = tfpd.Categorical(
logits=tf.py_func(_get_rewards, [
time, state[0], self._ground_truth,
self._ground_truth_length], tf.float32))
return tf.reshape(
sampler.sample(), (tf.shape(self._ground_truth)[0],))
sampler.sample(), (tf.shape(self._ground_truth)[0],))
6 changes: 3 additions & 3 deletions examples/text_style_transfer/ctrl_gen_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _build_model(self, inputs, vocab, gamma, lambda_g):
label_connector = MLPTransformConnector(self._hparams.dim_c)

# Gets the sentence representation: h = (c, z)
labels = tf.to_float(tf.reshape(inputs['labels'], [-1, 1]))
labels = tf.cast(tf.reshape(inputs['labels'], [-1, 1]), tf.float32)
c = label_connector(labels)
c_ = label_connector(1 - labels)
h = tf.concat([c, z], 1)
Expand Down Expand Up @@ -106,7 +106,7 @@ def _build_model(self, inputs, vocab, gamma, lambda_g):
inputs=clas_embedder(ids=inputs['text_ids'][:, 1:]),
sequence_length=inputs['length']-1)
loss_d_clas = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.to_float(inputs['labels']), logits=clas_logits)
labels=tf.cast(inputs['labels'], tf.float32), logits=clas_logits)
loss_d_clas = tf.reduce_mean(loss_d_clas)
accu_d = tx.evals.accuracy(labels=inputs['labels'], preds=clas_preds)

Expand All @@ -115,7 +115,7 @@ def _build_model(self, inputs, vocab, gamma, lambda_g):
inputs=clas_embedder(soft_ids=soft_outputs_.sample_id),
sequence_length=soft_length_)
loss_g_clas = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.to_float(1-inputs['labels']), logits=soft_logits)
labels=tf.cast(1-inputs['labels'], tf.float32), logits=soft_logits)
loss_g_clas = tf.reduce_mean(loss_g_clas)

# Accuracy on soft samples, for training progress monitoring
Expand Down
6 changes: 3 additions & 3 deletions examples/transformer/transformer_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import random
import os
import importlib
from torchtext import data
import tensorflow as tf
from torchtext import data
import texar as tx
from texar.modules import TransformerEncoder, TransformerDecoder
from texar.utils import transformer_utils
Expand Down Expand Up @@ -74,10 +74,10 @@ def main():
batch_size = tf.shape(encoder_input)[0]
# (text sequence length excluding padding)
encoder_input_length = tf.reduce_sum(
1 - tf.to_int32(tf.equal(encoder_input, 0)), axis=1)
1 - tf.cast(tf.equal(encoder_input, 0), tf.int32), axis=1)

labels = tf.placeholder(tf.int64, shape=(None, None))
is_target = tf.to_float(tf.not_equal(labels, 0))
is_target = tf.cast(tf.not_equal(labels, 0), tf.float32)

global_step = tf.Variable(0, dtype=tf.int64, trainable=False)
learning_rate = tf.placeholder(tf.float64, shape=(), name='lr')
Expand Down
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
tensorflow >= 1.7.0
tensorflow-gpu >= 1.7.0
tensorflow >= 1.10.0
tensorflow-gpu >= 1.10.0
tensorflow-probability >= 0.3.0
tensorflow-probability-gpu >= 0.3.0
funcsigs >= 1.0.2
funcsigs >= 1.0.2
packaging
15 changes: 8 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setuptools.setup(
name="texar",
version="0.2.1",
version="0.2.2",
url="https://github.com/asyml/texar",

description="Toolkit for Machine Learning and Text Generation",
Expand All @@ -26,19 +26,20 @@
platforms='any',

install_requires=[
'numpy',
'numpy<1.17.0',
'pyyaml',
'requests',
'funcsigs',
'funcsigs>=1.0.2',
'packaging'
],
extras_require={
'tensorflow-cpu': [
'tensorflow>=1.7.0',
'tensorflow-probability >= 0.3.0'
'tensorflow>=1.10.0,<2.0',
'tensorflow-probability>=0.3.0'
],
'tensorflow-gpu': [
'tensorflow-gpu>=1.7.0',
'tensorflow-probability-gpu >= 0.3.0'
'tensorflow-gpu>=1.10.0,<2.0',
'tensorflow-probability-gpu>=0.3.0'
]
},
package_data={
Expand Down
11 changes: 11 additions & 0 deletions texar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@
from __future__ import division
from __future__ import print_function


# pylint: disable=wildcard-import

import sys
from packaging import version
import tensorflow as tf


VERSION_WARNING = "1.13.2"

if version.parse(tf.__version__) <= version.parse(VERSION_WARNING):
tf.logging.set_verbosity(tf.logging.ERROR)
else:
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

if sys.version_info.major < 3:
# PY 2.x, import as is because Texar-PyTorch cannot be installed.
Expand Down
2 changes: 1 addition & 1 deletion texar/agents/dqn_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def _get_target_outputs(self, state_inputs):
return self._target(inputs=state_inputs, **self._qnet_caller_kwargs)

def _get_td_error(self, qnet_qvalues, actions, y):
return y - tf.reduce_sum(qnet_qvalues * tf.to_float(actions), axis=1)
return y - tf.reduce_sum(qnet_qvalues * tf.cast(actions, tf.float), axis=1)

def _get_train_op(self):
train_op = opt.get_train_op(
Expand Down
2 changes: 1 addition & 1 deletion texar/core/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def get_layer(hparams):
if not is_str(layer_type) and not isinstance(layer_type, type):
layer = layer_type
else:
layer_modules = ["tensorflow.layers", "texar.core", "texar.costum"]
layer_modules = ["tensorflow.layers", "texar.core", "texar.custom"]
layer_class = utils.check_or_get_class(layer_type, layer_modules)
if isinstance(hparams, dict):
default_kwargs = _layer_class_to_default_kwargs_map.get(layer_class,
Expand Down
7 changes: 4 additions & 3 deletions texar/core/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ def get_learning_rate_decay_fn(hparams=None):
if fn_kwargs is HParams:
fn_kwargs = fn_kwargs.todict()

start_step = tf.to_int32(hparams["start_decay_step"])
end_step = tf.to_int32(hparams["end_decay_step"])
start_step = tf.cast(hparams["start_decay_step"], tf.int32)
end_step = tf.cast(hparams["end_decay_step"], tf.int32)

def lr_decay_fn(learning_rate, global_step):
"""Learning rate decay function.
Expand All @@ -273,7 +273,8 @@ def lr_decay_fn(learning_rate, global_step):
scalar float Tensor: decayed learning rate.
"""
offset_global_step = tf.maximum(
tf.minimum(tf.to_int32(global_step), end_step) - start_step, 0)
tf.minimum(tf.cast(global_step, tf.int32), end_step) - start_step,
0)
if decay_fn == tf.train.piecewise_constant:
decayed_lr = decay_fn(x=offset_global_step, **fn_kwargs)
else:
Expand Down
Empty file added texar/custom/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions texar/data/data/tfrecord_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
import copy
import shutil
import tempfile
import ssl
import tensorflow as tf
import texar as tx


ssl._create_default_https_context = ssl._create_unverified_context


class TFRecordDataTest(tf.test.TestCase):
"""Tests tfrecord data class.
"""
Expand Down
2 changes: 1 addition & 1 deletion texar/data/data_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def decode(self, data, items):
if from_type is to_type:
continue
elif to_type is tf.string:
decoded_data[key] = tf.dtypes.as_string(decoded_data[key])
decoded_data[key] = tf.as_string(decoded_data[key])
elif from_type is tf.string:
decoded_data[key] = tf.string_to_number(
decoded_data[key], to_type)
Expand Down
2 changes: 1 addition & 1 deletion texar/data/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def map_ids_to_tokens(self, ids):
Returns:
A tensor of text tokens of the same shape.
"""
return self.id_to_token_map.lookup(tf.to_int64(ids))
return self.id_to_token_map.lookup(tf.cast(ids, tf.int64))

def map_tokens_to_ids(self, tokens):
"""Maps text tokens into ids.
Expand Down
6 changes: 3 additions & 3 deletions texar/evals/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def accuracy(labels, preds):
A float scalar Tensor containing the accuracy.
"""
labels = tf.cast(labels, preds.dtype)
return tf.reduce_mean(tf.to_float(tf.equal(preds, labels)))
return tf.reduce_mean(tf.cast(tf.equal(preds, labels), tf.float32))

def binary_clas_accuracy(pos_preds=None, neg_preds=None):
"""Calculates the accuracy of binary predictions.
Expand All @@ -44,7 +44,7 @@ def binary_clas_accuracy(pos_preds=None, neg_preds=None):
"""
pos_accu = accuracy(tf.ones_like(pos_preds), pos_preds)
neg_accu = accuracy(tf.zeros_like(neg_preds), neg_preds)
psize = tf.to_float(tf.size(pos_preds))
nsize = tf.to_float(tf.size(neg_preds))
psize = tf.cast(tf.size(pos_preds), tf.float32)
nsize = tf.cast(tf.size(neg_preds), tf.float32)
accu = (pos_accu * psize + neg_accu * nsize) / (psize + nsize)
return accu
2 changes: 1 addition & 1 deletion texar/losses/mle_losses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_sequence_sigmoid_cross_entropy(self):
labels = tf.placeholder(dtype=tf.int32, shape=None)
loss = tx.losses.sequence_sigmoid_cross_entropy(
logits=self._logits[:, :, 0],
labels=tf.to_float(labels),
labels=tf.cast(labels, tf.float32),
sequence_length=self._sequence_length)
with self.test_session() as sess:
rank = sess.run(
Expand Down
2 changes: 1 addition & 1 deletion texar/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
from texar.modules.policies import *
from texar.modules.qnets import *
from texar.modules.memory import *
from texar.modules.berts import *
from texar.modules.berts import *
2 changes: 1 addition & 1 deletion texar/modules/classifiers/bert_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _build(self,
else:
pred = tf.argmax(logits, axis=-1)
pred = tf.reshape(pred, [-1])
pred = tf.to_int64(pred)
pred = tf.cast(pred, tf.int64)

if not self._built:
self._add_internal_trainable_variables()
Expand Down
2 changes: 1 addition & 1 deletion texar/modules/classifiers/conv_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _build(self, # pylint: disable=arguments-differ
logits = tf.reshape(logits, [-1])
else:
pred = tf.argmax(logits, 1)
pred = tf.to_int64(tf.reshape(pred, [-1]))
pred = tf.cast(tf.reshape(pred, [-1]), tf.int64)

self._built = True

Expand Down
2 changes: 1 addition & 1 deletion texar/modules/classifiers/rnn_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _build(self,
else:
pred = tf.argmax(logits, axis=-1)
pred = tf.reshape(pred, [-1])
pred = tf.to_int64(pred)
pred = tf.cast(pred, tf.int64)

if not self._built:
self._add_internal_trainable_variables()
Expand Down
2 changes: 1 addition & 1 deletion texar/modules/connectors/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ class instance.
dstr = check_or_get_instance(
distribution, distribution_kwargs,
["tensorflow.distributions", "tensorflow_probability.distributions",
"tensorflow.contrib.distributions", "texar.custom"])
"texar.custom"])

if num_samples:
sample = dstr.sample(num_samples)
Expand Down
Loading

0 comments on commit 361fcdc

Please sign in to comment.