Skip to content

Commit 361fcdc

Browse files
authored
Enable PyPI installation
* Fix or toggle off warnings (mostly TF deprecation warnings) * Update dependency to TF>=1.10.0 * Upgrade to version v0.2.2
2 parents 81cb96a + 26c119f commit 361fcdc

37 files changed

+122
-80
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55

66
### Feature improvements
77

8+
### Fixes
9+
10+
## [v0.2.2](https://github.com/asyml/texar/releases/tag/v0.2.2) (2019-08-05)
11+
12+
### New features
13+
14+
* Enable installation from [Pypi](https://pypi.org/project/texar/). ([#186](https://github.com/asyml/texar/pull/186))
15+
16+
### Feature improvements
17+
818
* Use lazy import to be compatible with [texar-pytorch](https://github.com/asyml/texar-pytorch). ([#183](https://github.com/asyml/texar/pull/183))
919

1020
### Fixes

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
-----------------
66

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

8183
### Installation
84+
Besides the packages in requirements.txt, Texar additionally requires:
85+
86+
* tensorflow >= 1.10.0 (but <= 2.0). Follow the tensorflow official instructions to install the appropriate version
87+
88+
* tensorflow_probability >= 0.3.0. Follow the tensorflow_probability official instractions to install.
89+
90+
After Tensorflow is installed, please run the following commands to install Texar:
8291
```
8392
git clone https://github.com/asyml/texar.git
8493
cd texar

examples/bert/bert_classifier_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def main(_):
107107
input_ids = batch["input_ids"]
108108
segment_ids = batch["segment_ids"]
109109
batch_size = tf.shape(input_ids)[0]
110-
input_length = tf.reduce_sum(1 - tf.to_int32(tf.equal(input_ids, 0)),
110+
input_length = tf.reduce_sum(1 - tf.cast(tf.equal(input_ids, 0), tf.int32),
111111
axis=1)
112112
# Builds BERT
113113
with tf.variable_scope('bert'):
@@ -198,7 +198,7 @@ def _train_epoch(sess):
198198
periodically.
199199
"""
200200
iterator.restart_dataset(sess, 'train')
201-
201+
202202
fetches = {
203203
'train_op': train_op,
204204
'loss': loss,

examples/bert/bert_classifier_main_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def main(_):
8585
input_ids = batch["input_ids"]
8686
segment_ids = batch["segment_ids"]
8787
batch_size = tf.shape(input_ids)[0]
88-
input_length = tf.reduce_sum(1 - tf.to_int32(tf.equal(input_ids, 0)),
88+
input_length = tf.reduce_sum(1 - tf.cast(tf.equal(input_ids, 0), tf.int32),
8989
axis=1)
9090
# Builds BERT
9191
hparams = {

examples/seq2seq_exposure_bias/interpolation_helper.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import tensorflow as tf
2323
import numpy as np
2424

25+
from tensorflow_probability import distributions as tfpd
2526
from tensorflow.contrib.seq2seq import SampleEmbeddingHelper
2627
from texar.evals.bleu import sentence_bleu
2728
from rouge import Rouge
@@ -95,19 +96,19 @@ def sample(self, time, outputs, state, name=None):
9596
of 'state'([decoded_ids, rnn_state])
9697
"""
9798
sample_method_sampler = \
98-
tf.distributions.Categorical(probs=self._lambdas)
99+
tfpd.Categorical(probs=self._lambdas)
99100
sample_method_id = sample_method_sampler.sample()
100101

101102
truth_feeding = lambda: tf.cond(
102103
tf.less(time, tf.shape(self._ground_truth)[1]),
103-
lambda: tf.to_int32(self._ground_truth[:, time]),
104+
lambda: tf.cast(self._ground_truth[:, time], tf.int32),
104105
lambda: tf.ones_like(self._ground_truth[:, 0],
105106
dtype=tf.int32) * self._vocab.eos_token_id)
106107

107-
self_feeding = lambda : SampleEmbeddingHelper.sample(
108+
self_feeding = lambda: SampleEmbeddingHelper.sample(
108109
self, time, outputs, state, name)
109110

110-
reward_feeding = lambda : self._sample_by_reward(time, state)
111+
reward_feeding = lambda: self._sample_by_reward(time, state)
111112

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

208209
return result
209210

210-
sampler = tf.distributions.Categorical(
211+
sampler = tfpd.Categorical(
211212
logits=tf.py_func(_get_rewards, [
212213
time, state[0], self._ground_truth,
213214
self._ground_truth_length], tf.float32))
214215
return tf.reshape(
215-
sampler.sample(), (tf.shape(self._ground_truth)[0],))
216+
sampler.sample(), (tf.shape(self._ground_truth)[0],))

examples/text_style_transfer/ctrl_gen_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _build_model(self, inputs, vocab, gamma, lambda_g):
5555
label_connector = MLPTransformConnector(self._hparams.dim_c)
5656

5757
# Gets the sentence representation: h = (c, z)
58-
labels = tf.to_float(tf.reshape(inputs['labels'], [-1, 1]))
58+
labels = tf.cast(tf.reshape(inputs['labels'], [-1, 1]), tf.float32)
5959
c = label_connector(labels)
6060
c_ = label_connector(1 - labels)
6161
h = tf.concat([c, z], 1)
@@ -106,7 +106,7 @@ def _build_model(self, inputs, vocab, gamma, lambda_g):
106106
inputs=clas_embedder(ids=inputs['text_ids'][:, 1:]),
107107
sequence_length=inputs['length']-1)
108108
loss_d_clas = tf.nn.sigmoid_cross_entropy_with_logits(
109-
labels=tf.to_float(inputs['labels']), logits=clas_logits)
109+
labels=tf.cast(inputs['labels'], tf.float32), logits=clas_logits)
110110
loss_d_clas = tf.reduce_mean(loss_d_clas)
111111
accu_d = tx.evals.accuracy(labels=inputs['labels'], preds=clas_preds)
112112

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

121121
# Accuracy on soft samples, for training progress monitoring

examples/transformer/transformer_main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import random
2323
import os
2424
import importlib
25-
from torchtext import data
2625
import tensorflow as tf
26+
from torchtext import data
2727
import texar as tx
2828
from texar.modules import TransformerEncoder, TransformerDecoder
2929
from texar.utils import transformer_utils
@@ -74,10 +74,10 @@ def main():
7474
batch_size = tf.shape(encoder_input)[0]
7575
# (text sequence length excluding padding)
7676
encoder_input_length = tf.reduce_sum(
77-
1 - tf.to_int32(tf.equal(encoder_input, 0)), axis=1)
77+
1 - tf.cast(tf.equal(encoder_input, 0), tf.int32), axis=1)
7878

7979
labels = tf.placeholder(tf.int64, shape=(None, None))
80-
is_target = tf.to_float(tf.not_equal(labels, 0))
80+
is_target = tf.cast(tf.not_equal(labels, 0), tf.float32)
8181

8282
global_step = tf.Variable(0, dtype=tf.int64, trainable=False)
8383
learning_rate = tf.placeholder(tf.float64, shape=(), name='lr')

requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
tensorflow >= 1.7.0
2-
tensorflow-gpu >= 1.7.0
1+
tensorflow >= 1.10.0
2+
tensorflow-gpu >= 1.10.0
33
tensorflow-probability >= 0.3.0
44
tensorflow-probability-gpu >= 0.3.0
5-
funcsigs >= 1.0.2
5+
funcsigs >= 1.0.2
6+
packaging

setup.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
setuptools.setup(
1717
name="texar",
18-
version="0.2.1",
18+
version="0.2.2",
1919
url="https://github.com/asyml/texar",
2020

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

2828
install_requires=[
29-
'numpy',
29+
'numpy<1.17.0',
3030
'pyyaml',
3131
'requests',
32-
'funcsigs',
32+
'funcsigs>=1.0.2',
33+
'packaging'
3334
],
3435
extras_require={
3536
'tensorflow-cpu': [
36-
'tensorflow>=1.7.0',
37-
'tensorflow-probability >= 0.3.0'
37+
'tensorflow>=1.10.0,<2.0',
38+
'tensorflow-probability>=0.3.0'
3839
],
3940
'tensorflow-gpu': [
40-
'tensorflow-gpu>=1.7.0',
41-
'tensorflow-probability-gpu >= 0.3.0'
41+
'tensorflow-gpu>=1.10.0,<2.0',
42+
'tensorflow-probability-gpu>=0.3.0'
4243
]
4344
},
4445
package_data={

texar/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@
1919
from __future__ import division
2020
from __future__ import print_function
2121

22+
2223
# pylint: disable=wildcard-import
2324

2425
import sys
26+
from packaging import version
27+
import tensorflow as tf
28+
29+
30+
VERSION_WARNING = "1.13.2"
31+
32+
if version.parse(tf.__version__) <= version.parse(VERSION_WARNING):
33+
tf.logging.set_verbosity(tf.logging.ERROR)
34+
else:
35+
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
2536

2637
if sys.version_info.major < 3:
2738
# PY 2.x, import as is because Texar-PyTorch cannot be installed.

texar/agents/dqn_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def _get_target_outputs(self, state_inputs):
292292
return self._target(inputs=state_inputs, **self._qnet_caller_kwargs)
293293

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

297297
def _get_train_op(self):
298298
train_op = opt.get_train_op(

texar/core/layers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def get_layer(hparams):
573573
if not is_str(layer_type) and not isinstance(layer_type, type):
574574
layer = layer_type
575575
else:
576-
layer_modules = ["tensorflow.layers", "texar.core", "texar.costum"]
576+
layer_modules = ["tensorflow.layers", "texar.core", "texar.custom"]
577577
layer_class = utils.check_or_get_class(layer_type, layer_modules)
578578
if isinstance(hparams, dict):
579579
default_kwargs = _layer_class_to_default_kwargs_map.get(layer_class,

texar/core/optimization.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ def get_learning_rate_decay_fn(hparams=None):
259259
if fn_kwargs is HParams:
260260
fn_kwargs = fn_kwargs.todict()
261261

262-
start_step = tf.to_int32(hparams["start_decay_step"])
263-
end_step = tf.to_int32(hparams["end_decay_step"])
262+
start_step = tf.cast(hparams["start_decay_step"], tf.int32)
263+
end_step = tf.cast(hparams["end_decay_step"], tf.int32)
264264

265265
def lr_decay_fn(learning_rate, global_step):
266266
"""Learning rate decay function.
@@ -273,7 +273,8 @@ def lr_decay_fn(learning_rate, global_step):
273273
scalar float Tensor: decayed learning rate.
274274
"""
275275
offset_global_step = tf.maximum(
276-
tf.minimum(tf.to_int32(global_step), end_step) - start_step, 0)
276+
tf.minimum(tf.cast(global_step, tf.int32), end_step) - start_step,
277+
0)
277278
if decay_fn == tf.train.piecewise_constant:
278279
decayed_lr = decay_fn(x=offset_global_step, **fn_kwargs)
279280
else:

texar/custom/__init__.py

Whitespace-only changes.

texar/data/data/tfrecord_data_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
import copy
1515
import shutil
1616
import tempfile
17+
import ssl
1718
import tensorflow as tf
1819
import texar as tx
1920

2021

22+
ssl._create_default_https_context = ssl._create_unverified_context
23+
24+
2125
class TFRecordDataTest(tf.test.TestCase):
2226
"""Tests tfrecord data class.
2327
"""

texar/data/data_decoders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def decode(self, data, items):
630630
if from_type is to_type:
631631
continue
632632
elif to_type is tf.string:
633-
decoded_data[key] = tf.dtypes.as_string(decoded_data[key])
633+
decoded_data[key] = tf.as_string(decoded_data[key])
634634
elif from_type is tf.string:
635635
decoded_data[key] = tf.string_to_number(
636636
decoded_data[key], to_type)

texar/data/vocabulary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def map_ids_to_tokens(self, ids):
182182
Returns:
183183
A tensor of text tokens of the same shape.
184184
"""
185-
return self.id_to_token_map.lookup(tf.to_int64(ids))
185+
return self.id_to_token_map.lookup(tf.cast(ids, tf.int64))
186186

187187
def map_tokens_to_ids(self, tokens):
188188
"""Maps text tokens into ids.

texar/evals/metrics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def accuracy(labels, preds):
2626
A float scalar Tensor containing the accuracy.
2727
"""
2828
labels = tf.cast(labels, preds.dtype)
29-
return tf.reduce_mean(tf.to_float(tf.equal(preds, labels)))
29+
return tf.reduce_mean(tf.cast(tf.equal(preds, labels), tf.float32))
3030

3131
def binary_clas_accuracy(pos_preds=None, neg_preds=None):
3232
"""Calculates the accuracy of binary predictions.
@@ -44,7 +44,7 @@ def binary_clas_accuracy(pos_preds=None, neg_preds=None):
4444
"""
4545
pos_accu = accuracy(tf.ones_like(pos_preds), pos_preds)
4646
neg_accu = accuracy(tf.zeros_like(neg_preds), neg_preds)
47-
psize = tf.to_float(tf.size(pos_preds))
48-
nsize = tf.to_float(tf.size(neg_preds))
47+
psize = tf.cast(tf.size(pos_preds), tf.float32)
48+
nsize = tf.cast(tf.size(neg_preds), tf.float32)
4949
accu = (pos_accu * psize + neg_accu * nsize) / (psize + nsize)
5050
return accu

texar/losses/mle_losses_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_sequence_sigmoid_cross_entropy(self):
102102
labels = tf.placeholder(dtype=tf.int32, shape=None)
103103
loss = tx.losses.sequence_sigmoid_cross_entropy(
104104
logits=self._logits[:, :, 0],
105-
labels=tf.to_float(labels),
105+
labels=tf.cast(labels, tf.float32),
106106
sequence_length=self._sequence_length)
107107
with self.test_session() as sess:
108108
rank = sess.run(

texar/modules/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
from texar.modules.policies import *
3131
from texar.modules.qnets import *
3232
from texar.modules.memory import *
33-
from texar.modules.berts import *
33+
from texar.modules.berts import *

texar/modules/classifiers/bert_classifiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def _build(self,
273273
else:
274274
pred = tf.argmax(logits, axis=-1)
275275
pred = tf.reshape(pred, [-1])
276-
pred = tf.to_int64(pred)
276+
pred = tf.cast(pred, tf.int64)
277277

278278
if not self._built:
279279
self._add_internal_trainable_variables()

texar/modules/classifiers/conv_classifiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _build(self, # pylint: disable=arguments-differ
189189
logits = tf.reshape(logits, [-1])
190190
else:
191191
pred = tf.argmax(logits, 1)
192-
pred = tf.to_int64(tf.reshape(pred, [-1]))
192+
pred = tf.cast(tf.reshape(pred, [-1]), tf.int64)
193193

194194
self._built = True
195195

texar/modules/classifiers/rnn_classifiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def _build(self,
328328
else:
329329
pred = tf.argmax(logits, axis=-1)
330330
pred = tf.reshape(pred, [-1])
331-
pred = tf.to_int64(pred)
331+
pred = tf.cast(pred, tf.int64)
332332

333333
if not self._built:
334334
self._add_internal_trainable_variables()

texar/modules/connectors/connectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ class instance.
664664
dstr = check_or_get_instance(
665665
distribution, distribution_kwargs,
666666
["tensorflow.distributions", "tensorflow_probability.distributions",
667-
"tensorflow.contrib.distributions", "texar.custom"])
667+
"texar.custom"])
668668

669669
if num_samples:
670670
sample = dstr.sample(num_samples)

0 commit comments

Comments
 (0)