提交 cd53f3c3 编写于 作者: V Vijay Vasudevan

TensorFlow: upstrea changes from git.

Change 109628097
	Fix gcc 4.8.1 compile

	Modified from patch by @assolini here:

	https://github.com/tensorflow/tensorflow/issues/405
Change 109624275
	Make preview frame ImageReader global so that it does not get GC'd.

	This may fix an issue with connecting to the camera on some devices where the underlying Surface is prematurely cleaned up (http://stackoverflow.com/questions/33437961/android-camera-2-api-bufferqueue-has-been-abandoned).
Change 109620599
	- improved test a little to make it easier to understand as it serves as an
	example for users
Change 109614953
	TensorFlow: update tutorials/howtos to point to correct
	location of files, show python example in addition to bazel.
Change 109612732
	TensorFlow: move reading_data into examples, change data dir
	to /tmp/data.  Validated that they all run, but these
	probably need a selftest at some point.
Change 109608695
	Apply 'gate_gradients' only when there is more than one real gradients.
Change 109605014
	There are 3 obvious places to start using TensorFlow. 2/3 of the starting points do not have a link to the installation instructions.
Change 109604287
	Make the `tf.reshape` shape function more restrictive.

	Previously, it did not raise a construction-time error if the input
	shape and the new shape were incompatible; now it detects this and
	raises a `ValueError`.
Change 109603375
	TensorFlow: Move word2vec_basic.py from g3doc/ to examples/

	There are no additional libraries this uses, so nothing else
	needs to be done
Change 109601289
	TensorBoard tag 3
Change 109600908
	Decrease number of scalar values stored by TensorBoard.
	10k is more than displays nicely.
Change 109599464
	Fix "smart restart" functionality in TensorBoard (it throws away dead data)
	After restarts, a file_version event is created that always has step 0.
	We need to ignore this.
Change 109597667
	Switch to using /dev/urandom for TensorFlow randomness.

	Using /dev/random leads to slowdown when running in an environment
	with poor access to an entropy source (such as some VMs). /dev/urandom
	has more predictable performance, and we don't require
	cryptographically secure random number generation, so a PRNG is good
	enough.

	Also removes the use of the RNG in DirectSession construction. This
	was being used to generate a session handle, which is not necessary
	(since a DirectSession owns its devices, we don't need a unique handle
	to key the OpSegment objects registered with the various devices).

	This addresses bugs that have been reported on the mailing list and
	Stack Overflow.
Change 109596906
	Add an is_unsigned property to dtype
Change 109596830
	Remove unnecessary fill in clip_by_value
Change 109591880
	Remove Android demo's libpthread.so dummy file (required by protobuf) from repo and generate it at compile-time.

	This makes the Android demo more portable, as the generated file will now always be the correct archictecture for linking.
Change 109589028
	Isolating out the RTTI part of TensorFlow and add non-RTTI backups for Android.
	This saves about 400KB of the compiled library, when compiling the Android
	tensorflow target with -fno-rtti.
Change 109589018
	Internal reworking of LSTMCell.
Change 109588229
	Allow bool-valued tensors to be persisted.
Change 109577175
	TensorBoard host defaults to 0.0.0.0
Change 109551438
	TensorFlow: move mnist g3doc tutorials into tensorflow/examples.

	Update examples to point to the correct location.

	Adds tests to make sure they don't regress, do some lint cleanup.

Base CL: 109630240
上级 11e3d0fa
...@@ -35,7 +35,6 @@ limitations under the License. ...@@ -35,7 +35,6 @@ limitations under the License.
#include "tensorflow/core/lib/core/threadpool.h" #include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/gtl/array_slice.h" #include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/lib/gtl/stl_util.h" #include "tensorflow/core/lib/gtl/stl_util.h"
#include "tensorflow/core/lib/random/random.h"
#include "tensorflow/core/lib/strings/numbers.h" #include "tensorflow/core/lib/strings/numbers.h"
#include "tensorflow/core/lib/strings/str_util.h" #include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/lib/strings/strcat.h" #include "tensorflow/core/lib/strings/strcat.h"
...@@ -114,7 +113,10 @@ DirectSession::DirectSession(const SessionOptions& options, ...@@ -114,7 +113,10 @@ DirectSession::DirectSession(const SessionOptions& options,
cancellation_manager_(new CancellationManager()) { cancellation_manager_(new CancellationManager()) {
static bool init = InitModule(options); static bool init = InitModule(options);
CHECK(init); // Avoids compiler warning that init is unused. CHECK(init); // Avoids compiler warning that init is unused.
session_handle_ = strings::FpToString(random::New64()); // NOTE(mrry): We do not need to use a unique string for the session
// handle, because DirectSession owns its devices. This may change
// in future versions.
session_handle_ = "direct";
int devices_added = 0; int devices_added = 0;
if (options.config.log_device_placement()) { if (options.config.log_device_placement()) {
const string mapping_str = device_mgr_->DeviceMappingString(); const string mapping_str = device_mgr_->DeviceMappingString();
......
...@@ -41,7 +41,7 @@ void ResourceMgr::Clear() { ...@@ -41,7 +41,7 @@ void ResourceMgr::Clear() {
containers_.clear(); containers_.clear();
} }
Status ResourceMgr::DoCreate(const string& container, std::type_index type, Status ResourceMgr::DoCreate(const string& container, ResourceMgrTypeIndex type,
const string& name, ResourceBase* resource) { const string& name, ResourceBase* resource) {
{ {
mutex_lock l(mu_); mutex_lock l(mu_);
...@@ -58,7 +58,7 @@ Status ResourceMgr::DoCreate(const string& container, std::type_index type, ...@@ -58,7 +58,7 @@ Status ResourceMgr::DoCreate(const string& container, std::type_index type,
type.name()); type.name());
} }
Status ResourceMgr::DoLookup(const string& container, std::type_index type, Status ResourceMgr::DoLookup(const string& container, ResourceMgrTypeIndex type,
const string& name, const string& name,
ResourceBase** resource) const { ResourceBase** resource) const {
mutex_lock l(mu_); mutex_lock l(mu_);
...@@ -76,7 +76,7 @@ Status ResourceMgr::DoLookup(const string& container, std::type_index type, ...@@ -76,7 +76,7 @@ Status ResourceMgr::DoLookup(const string& container, std::type_index type,
return Status::OK(); return Status::OK();
} }
Status ResourceMgr::DoDelete(const string& container, std::type_index type, Status ResourceMgr::DoDelete(const string& container, ResourceMgrTypeIndex type,
const string& name) { const string& name) {
ResourceBase* base = nullptr; ResourceBase* base = nullptr;
{ {
......
...@@ -71,6 +71,65 @@ class ResourceBase : public core::RefCounted { ...@@ -71,6 +71,65 @@ class ResourceBase : public core::RefCounted {
virtual string DebugString() = 0; virtual string DebugString() = 0;
}; };
// On Android, we would like to avoid using RTTI for smaller binary sizes. The
// following #ifdef section provides a non-functional replacement for
// std::type_index (with a minimal set of functions needed by ResourceMgr).
#ifdef __ANDROID__
// A thin TypeIndex class that mimics std::type_index but does not use RTTI. As
// a result, it does not provide the actual name of the type, and only returns a
// pre-baked string specifying that RTTI is disabled.
// The hash code provided in this class is unique for each class. However, it is
// generated at runtime so this hash code should not be serialized - the value
// for the same type can change from different runs.
class ResourceMgrTypeIndex {
public:
ResourceMgrTypeIndex(const ResourceMgrTypeIndex& src) : hash_(src.hash_) {}
ResourceMgrTypeIndex& operator=(const ResourceMgrTypeIndex& src) {
hash_ = src.hash_;
return *this;
}
bool operator==(const ResourceMgrTypeIndex& rhs) const {
return (hash_ == rhs.hash_);
}
bool operator!=(const ResourceMgrTypeIndex& rhs) const {
return (hash_ != rhs.hash_);
}
~ResourceMgrTypeIndex() {}
string name() const { return "[RTTI disabled for Android]"; }
uint64 hash_code() const { return hash_; }
// Returns a ResourceMgrTypeIndex object that corresponds to a typename.
template <typename T>
static ResourceMgrTypeIndex Make() {
static bool hash_bit[1];
return ResourceMgrTypeIndex(
static_cast<uint64>(reinterpret_cast<intptr_t>(hash_bit)));
}
private:
// We hide the constructor to be private. One needs to create the templated
// Make<T>() function to create a ResourceMgrTypeIndex object.
ResourceMgrTypeIndex(const uint64 hash) : hash_(hash) {}
uint64 hash_;
};
template <typename T>
inline ResourceMgrTypeIndex GetResourceMgrTypeIndex() {
return ResourceMgrTypeIndex::Make<T>();
}
#else // __ANDROID__
typedef std::type_index ResourceMgrTypeIndex;
template <typename T>
inline ResourceMgrTypeIndex GetResourceMgrTypeIndex() {
return ResourceMgrTypeIndex(typeid(T));
}
#endif // __ANDROID__
class ResourceMgr { class ResourceMgr {
public: public:
ResourceMgr(); ResourceMgr();
...@@ -122,7 +181,7 @@ class ResourceMgr { ...@@ -122,7 +181,7 @@ class ResourceMgr {
void Clear(); void Clear();
private: private:
typedef std::pair<std::type_index, string> Key; typedef std::pair<ResourceMgrTypeIndex, string> Key;
struct KeyHash { struct KeyHash {
std::size_t operator()(const Key& k) const { std::size_t operator()(const Key& k) const {
return Hash64(k.second.data(), k.second.size(), k.first.hash_code()); return Hash64(k.second.data(), k.second.size(), k.first.hash_code());
...@@ -139,13 +198,13 @@ class ResourceMgr { ...@@ -139,13 +198,13 @@ class ResourceMgr {
mutable mutex mu_; mutable mutex mu_;
std::unordered_map<string, Container*> containers_ GUARDED_BY(mu_); std::unordered_map<string, Container*> containers_ GUARDED_BY(mu_);
Status DoCreate(const string& container, std::type_index type, Status DoCreate(const string& container, ResourceMgrTypeIndex type,
const string& name, const string& name,
ResourceBase* resource) TF_MUST_USE_RESULT; ResourceBase* resource) TF_MUST_USE_RESULT;
Status DoLookup(const string& container, std::type_index type, Status DoLookup(const string& container, ResourceMgrTypeIndex type,
const string& name, const string& name,
ResourceBase** resource) const TF_MUST_USE_RESULT; ResourceBase** resource) const TF_MUST_USE_RESULT;
Status DoDelete(const string& container, std::type_index type, Status DoDelete(const string& container, ResourceMgrTypeIndex type,
const string& name) TF_MUST_USE_RESULT; const string& name) TF_MUST_USE_RESULT;
TF_DISALLOW_COPY_AND_ASSIGN(ResourceMgr); TF_DISALLOW_COPY_AND_ASSIGN(ResourceMgr);
...@@ -223,7 +282,7 @@ Status ResourceMgr::Create(const string& container, const string& name, ...@@ -223,7 +282,7 @@ Status ResourceMgr::Create(const string& container, const string& name,
T* resource) { T* resource) {
CheckDeriveFromResourceBase<T>(); CheckDeriveFromResourceBase<T>();
CHECK(resource != nullptr); CHECK(resource != nullptr);
return DoCreate(container, std::type_index(typeid(T)), name, resource); return DoCreate(container, GetResourceMgrTypeIndex<T>(), name, resource);
} }
template <typename T> template <typename T>
...@@ -231,7 +290,7 @@ Status ResourceMgr::Lookup(const string& container, const string& name, ...@@ -231,7 +290,7 @@ Status ResourceMgr::Lookup(const string& container, const string& name,
T** resource) const { T** resource) const {
CheckDeriveFromResourceBase<T>(); CheckDeriveFromResourceBase<T>();
ResourceBase* found = nullptr; ResourceBase* found = nullptr;
Status s = DoLookup(container, std::type_index(typeid(T)), name, &found); Status s = DoLookup(container, GetResourceMgrTypeIndex<T>(), name, &found);
if (s.ok()) { if (s.ok()) {
// It's safe to down cast 'found' to T* since // It's safe to down cast 'found' to T* since
// typeid(T).hash_code() is part of the map key. // typeid(T).hash_code() is part of the map key.
...@@ -265,7 +324,7 @@ Status ResourceMgr::LookupOrCreate(const string& container, const string& name, ...@@ -265,7 +324,7 @@ Status ResourceMgr::LookupOrCreate(const string& container, const string& name,
template <typename T> template <typename T>
Status ResourceMgr::Delete(const string& container, const string& name) { Status ResourceMgr::Delete(const string& container, const string& name) {
CheckDeriveFromResourceBase<T>(); CheckDeriveFromResourceBase<T>();
return DoDelete(container, std::type_index(typeid(T)), name); return DoDelete(container, GetResourceMgrTypeIndex<T>(), name);
} }
template <typename T> template <typename T>
......
...@@ -148,6 +148,7 @@ void SaveTensors( ...@@ -148,6 +148,7 @@ void SaveTensors(
break break
switch (input.dtype()) { switch (input.dtype()) {
WRITER_ADD(DT_BOOL);
WRITER_ADD(DT_FLOAT); WRITER_ADD(DT_FLOAT);
WRITER_ADD(DT_DOUBLE); WRITER_ADD(DT_DOUBLE);
WRITER_ADD(DT_INT32); WRITER_ADD(DT_INT32);
...@@ -269,6 +270,7 @@ void RestoreTensor(OpKernelContext* context, ...@@ -269,6 +270,7 @@ void RestoreTensor(OpKernelContext* context,
break break
switch (type) { switch (type) {
READER_COPY(DT_BOOL);
READER_COPY(DT_FLOAT); READER_COPY(DT_FLOAT);
READER_COPY(DT_DOUBLE); READER_COPY(DT_DOUBLE);
READER_COPY(DT_INT32); READER_COPY(DT_INT32);
......
...@@ -51,11 +51,10 @@ class MatrixInverseOp ...@@ -51,11 +51,10 @@ class MatrixInverseOp
} }
} }
using typename UnaryLinearAlgebraOp<Scalar, SupportsBatchOperationT>::Matrix; typedef UnaryLinearAlgebraOp<Scalar, SupportsBatchOperationT> Base;
using using Matrix = typename Base::Matrix;
typename UnaryLinearAlgebraOp<Scalar, SupportsBatchOperationT>::MatrixMap; using MatrixMap = typename Base::MatrixMap;
using typename UnaryLinearAlgebraOp<Scalar, using ConstMatrixMap = typename Base::ConstMatrixMap;
SupportsBatchOperationT>::ConstMatrixMap;
void ComputeMatrix(OpKernelContext* context, const ConstMatrixMap& input, void ComputeMatrix(OpKernelContext* context, const ConstMatrixMap& input,
MatrixMap* output) override { MatrixMap* output) override {
......
...@@ -44,8 +44,8 @@ class SaveOpTest : public OpsTestBase { ...@@ -44,8 +44,8 @@ class SaveOpTest : public OpsTestBase {
ASSERT_OK(NodeDefBuilder("myop", "Save") ASSERT_OK(NodeDefBuilder("myop", "Save")
.Input(FakeInput()) .Input(FakeInput())
.Input(FakeInput()) .Input(FakeInput())
.Input(FakeInput( .Input(FakeInput({DT_BOOL, DT_INT32, DT_FLOAT, DT_DOUBLE,
{DT_INT32, DT_FLOAT, DT_DOUBLE, DT_QINT8, DT_QINT32})) DT_QINT8, DT_QINT32}))
.Finalize(node_def())); .Finalize(node_def()));
ASSERT_OK(InitOp()); ASSERT_OK(InitOp());
} }
...@@ -53,7 +53,8 @@ class SaveOpTest : public OpsTestBase { ...@@ -53,7 +53,8 @@ class SaveOpTest : public OpsTestBase {
TEST_F(SaveOpTest, Simple) { TEST_F(SaveOpTest, Simple) {
const string filename = io::JoinPath(testing::TmpDir(), "tensor_simple"); const string filename = io::JoinPath(testing::TmpDir(), "tensor_simple");
const string tensornames[] = {"tensor_int", "tensor_float", "tensor_double", const string tensornames[] = {"tensor_bool", "tensor_int",
"tensor_float", "tensor_double",
"tensor_qint8", "tensor_qint32"}; "tensor_qint8", "tensor_qint32"};
MakeOp(); MakeOp();
...@@ -62,9 +63,12 @@ TEST_F(SaveOpTest, Simple) { ...@@ -62,9 +63,12 @@ TEST_F(SaveOpTest, Simple) {
[&filename](int x) -> string { return filename; }); [&filename](int x) -> string { return filename; });
// Add the tensor names // Add the tensor names
AddInput<string>(TensorShape({5}), AddInput<string>(TensorShape({6}),
[&tensornames](int x) -> string { return tensornames[x]; }); [&tensornames](int x) -> string { return tensornames[x]; });
// Add a 1-d bool tensor
AddInput<bool>(TensorShape({2}), [](int x) -> bool { return x != 0; });
// Add a 1-d integer tensor // Add a 1-d integer tensor
AddInput<int32>(TensorShape({10}), [](int x) -> int32 { return x + 1; }); AddInput<int32>(TensorShape({10}), [](int x) -> int32 { return x + 1; });
...@@ -93,6 +97,25 @@ TEST_F(SaveOpTest, Simple) { ...@@ -93,6 +97,25 @@ TEST_F(SaveOpTest, Simple) {
EXPECT_OK(reader.status()); EXPECT_OK(reader.status());
// We expect to find all saved tensors // We expect to find all saved tensors
{
// The 1-d bool tensor
TensorShape shape;
DataType type;
EXPECT_TRUE(reader.HasTensor("tensor_bool", &shape, &type));
TensorShape expected({2});
EXPECT_TRUE(shape.IsSameSize(expected));
EXPECT_EQ(DT_BOOL, type);
// We expect the tensor value to be correct.
TensorSlice s = TensorSlice::ParseOrDie("-");
bool data[2];
std::fill_n(data, 2, false);
EXPECT_TRUE(reader.CopySliceData("tensor_bool", s, data));
for (int i = 0; i < 2; ++i) {
EXPECT_EQ((i != 0), data[i]);
}
}
{ {
// The 1-d integer tensor // The 1-d integer tensor
TensorShape shape; TensorShape shape;
......
...@@ -132,9 +132,11 @@ template <DataType DT, int NDIM> ...@@ -132,9 +132,11 @@ template <DataType DT, int NDIM>
inline void TileOp<Device>::HandleCase( inline void TileOp<Device>::HandleCase(
OpKernelContext* context, const gtl::ArraySlice<int32>& multiples_array, OpKernelContext* context, const gtl::ArraySlice<int32>& multiples_array,
Tensor* result) { Tensor* result) {
// TODO(vrv): print out the device name if useful. Currently disabled to avoid
// having to use RTTI.
LOG(FATAL) << "TileOp: Invalid combination of Device, DT and NDIM: " LOG(FATAL) << "TileOp: Invalid combination of Device, DT and NDIM: "
<< typeid(Device).name() << ", " << DataTypeString(DT) << ", " // << typeid(Device).name() << ", "
<< NDIM; << DataTypeString(DT) << ", " << NDIM;
} }
#define HANDLE_CASE(device, dtype, ndim) \ #define HANDLE_CASE(device, dtype, ndim) \
...@@ -353,7 +355,11 @@ inline void TileGradientOp<Device>::HandleCase( ...@@ -353,7 +355,11 @@ inline void TileGradientOp<Device>::HandleCase(
OpKernelContext* context, const std::vector<int32>& input_dims, OpKernelContext* context, const std::vector<int32>& input_dims,
const gtl::ArraySlice<int32>& multiples_array, Tensor* result) { const gtl::ArraySlice<int32>& multiples_array, Tensor* result) {
LOG(FATAL) << "TileGradientOp: Invalid combination of Device, DT and NDIM: " LOG(FATAL) << "TileGradientOp: Invalid combination of Device, DT and NDIM: "
#ifdef __ANDROID__
<< "[Device not shown, no RTTI], " << DataTypeString(DT) << ", "
#else // __ANDROID__
<< typeid(Device).name() << ", " << DataTypeString(DT) << ", " << typeid(Device).name() << ", " << DataTypeString(DT) << ", "
#endif // __ANDROID__
<< NDIM; << NDIM;
} }
......
...@@ -22,7 +22,7 @@ namespace tensorflow { ...@@ -22,7 +22,7 @@ namespace tensorflow {
namespace random { namespace random {
std::mt19937_64* InitRng() { std::mt19937_64* InitRng() {
std::random_device device("/dev/random"); std::random_device device("/dev/urandom");
return new std::mt19937_64(device()); return new std::mt19937_64(device());
} }
......
...@@ -22,7 +22,7 @@ REGISTER_OP("Save") ...@@ -22,7 +22,7 @@ REGISTER_OP("Save")
.Input("filename: string") .Input("filename: string")
.Input("tensor_names: string") .Input("tensor_names: string")
.Input("data: T") .Input("data: T")
.Attr("T: list({float, double, int32, int64, quint8, qint8, qint32})") .Attr("T: list({bool, float, double, int32, int64, quint8, qint8, qint32})")
.Doc(R"doc( .Doc(R"doc(
Saves the input tensors to disk. Saves the input tensors to disk.
...@@ -42,7 +42,7 @@ REGISTER_OP("SaveSlices") ...@@ -42,7 +42,7 @@ REGISTER_OP("SaveSlices")
.Input("tensor_names: string") .Input("tensor_names: string")
.Input("shapes_and_slices: string") .Input("shapes_and_slices: string")
.Input("data: T") .Input("data: T")
.Attr("T: list({float, double, int32, int64, quint8, qint8, qint32})") .Attr("T: list({bool, float, double, int32, int64, quint8, qint8, qint32})")
.Doc(R"doc( .Doc(R"doc(
Saves input tensors slices to disk. Saves input tensors slices to disk.
......
...@@ -6163,6 +6163,7 @@ op { ...@@ -6163,6 +6163,7 @@ op {
minimum: 1 minimum: 1
allowed_values { allowed_values {
list { list {
type: DT_BOOL
type: DT_FLOAT type: DT_FLOAT
type: DT_DOUBLE type: DT_DOUBLE
type: DT_INT32 type: DT_INT32
...@@ -6205,6 +6206,7 @@ op { ...@@ -6205,6 +6206,7 @@ op {
minimum: 1 minimum: 1
allowed_values { allowed_values {
list { list {
type: DT_BOOL
type: DT_FLOAT type: DT_FLOAT
type: DT_DOUBLE type: DT_DOUBLE
type: DT_INT32 type: DT_INT32
......
...@@ -90,6 +90,7 @@ void Fill(T* data, size_t n, TensorProto* t); ...@@ -90,6 +90,7 @@ void Fill(T* data, size_t n, TensorProto* t);
t->mutable_##FIELD##_val()->Swap(&copy); \ t->mutable_##FIELD##_val()->Swap(&copy); \
} }
TENSOR_PROTO_EXTRACT_TYPE(bool, bool, bool);
TENSOR_PROTO_EXTRACT_TYPE(float, float, float); TENSOR_PROTO_EXTRACT_TYPE(float, float, float);
TENSOR_PROTO_EXTRACT_TYPE(double, double, double); TENSOR_PROTO_EXTRACT_TYPE(double, double, double);
TENSOR_PROTO_EXTRACT_TYPE(int32, int, int32); TENSOR_PROTO_EXTRACT_TYPE(int32, int, int32);
......
...@@ -52,9 +52,17 @@ const TensorSliceReader* TensorSliceReaderCache::GetReader( ...@@ -52,9 +52,17 @@ const TensorSliceReader* TensorSliceReaderCache::GetReader(
TensorSliceReader::OpenTableFunction open_function, int preferred_shard) { TensorSliceReader::OpenTableFunction open_function, int preferred_shard) {
mutex_lock l(mu_); mutex_lock l(mu_);
#ifdef ANDROID
// On Android, we have RTTI disabled so we will hard-code func_ptr to be zero,
// since we cannot figure out the target type for open_function.
// TODO(jiayq): find a more elegant way to possibly enable cache again.
TensorSliceReaderCache::OpenFuncType* func_ptr = nullptr;
#else // ANDROID
// Get the function pointer from the open_function value. // Get the function pointer from the open_function value.
TensorSliceReaderCache::OpenFuncType* func_ptr = TensorSliceReaderCache::OpenFuncType* func_ptr =
open_function.target<TensorSliceReaderCache::OpenFuncType>(); open_function.target<TensorSliceReaderCache::OpenFuncType>();
#endif
if (!func_ptr) { if (!func_ptr) {
// We could not get the pointer, no caching is possible. // We could not get the pointer, no caching is possible.
LOG(WARNING) << "Caching disabled because the open function is a lambda."; LOG(WARNING) << "Caching disabled because the open function is a lambda.";
......
...@@ -11,7 +11,7 @@ exports_files(["LICENSE"]) ...@@ -11,7 +11,7 @@ exports_files(["LICENSE"])
cc_library( cc_library(
name = "tensorflow_native_libs", name = "tensorflow_native_libs",
srcs = glob(["jni/**/*.cc"]), srcs = glob(["jni/**/*.cc"]) + [":libpthread.so"],
hdrs = glob(["jni/**/*.h"]), hdrs = glob(["jni/**/*.h"]),
copts = [ copts = [
"-std=c++11", "-std=c++11",
...@@ -23,18 +23,20 @@ cc_library( ...@@ -23,18 +23,20 @@ cc_library(
"manual", "manual",
"notap", "notap",
], ],
deps = [ deps = ["//tensorflow/core:android_tensorflow_lib"],
":dummy_pthread",
"//tensorflow/core:android_tensorflow_lib",
],
) )
# This library only exists as a workaround to satisfy dependencies # This library only exists as a workaround to satisfy dependencies
# that declare -lpthread in their linkopts. Although Android supports # that declare -lpthread in their linkopts. Although Android supports
# pthreads, it does not provide it as a separate library. # pthreads, it does not provide it as a separate library.
cc_library( cc_binary(
name = "dummy_pthread", name = "libpthread.so",
srcs = ["jni/libpthread.so"], srcs = [],
linkopts = ["-shared"],
tags = [
"manual",
"notap",
],
) )
android_binary( android_binary(
......
...@@ -180,9 +180,9 @@ public class CameraConnectionFragment extends Fragment { ...@@ -180,9 +180,9 @@ public class CameraConnectionFragment extends Fragment {
private Handler backgroundHandler; private Handler backgroundHandler;
/** /**
* An {@link ImageReader} that handles still image capture. * An {@link ImageReader} that handles preview frame capture.
*/ */
private ImageReader imageReader; private ImageReader previewReader;
/** /**
* {@link android.hardware.camera2.CaptureRequest.Builder} for the camera preview * {@link android.hardware.camera2.CaptureRequest.Builder} for the camera preview
...@@ -328,10 +328,6 @@ public class CameraConnectionFragment extends Fragment { ...@@ -328,10 +328,6 @@ public class CameraConnectionFragment extends Fragment {
Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888)), Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888)),
new CompareSizesByArea()); new CompareSizesByArea());
imageReader =
ImageReader.newInstance(
largest.getWidth(), largest.getHeight(), ImageFormat.YUV_420_888, /*maxImages*/ 2);
// Danger, W.R.! Attempting to use too large a preview size could exceed the camera // Danger, W.R.! Attempting to use too large a preview size could exceed the camera
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
// garbage capture data. // garbage capture data.
...@@ -393,9 +389,9 @@ public class CameraConnectionFragment extends Fragment { ...@@ -393,9 +389,9 @@ public class CameraConnectionFragment extends Fragment {
cameraDevice.close(); cameraDevice.close();
cameraDevice = null; cameraDevice = null;
} }
if (null != imageReader) { if (null != previewReader) {
imageReader.close(); previewReader.close();
imageReader = null; previewReader = null;
} }
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e); throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
...@@ -465,7 +461,7 @@ public class CameraConnectionFragment extends Fragment { ...@@ -465,7 +461,7 @@ public class CameraConnectionFragment extends Fragment {
LOGGER.i("Opening camera preview: " + previewSize.getWidth() + "x" + previewSize.getHeight()); LOGGER.i("Opening camera preview: " + previewSize.getWidth() + "x" + previewSize.getHeight());
// Create the reader for the preview frames. // Create the reader for the preview frames.
final ImageReader previewReader = previewReader =
ImageReader.newInstance( ImageReader.newInstance(
previewSize.getWidth(), previewSize.getHeight(), ImageFormat.YUV_420_888, 2); previewSize.getWidth(), previewSize.getHeight(), ImageFormat.YUV_420_888, 2);
...@@ -474,7 +470,7 @@ public class CameraConnectionFragment extends Fragment { ...@@ -474,7 +470,7 @@ public class CameraConnectionFragment extends Fragment {
// Here, we create a CameraCaptureSession for camera preview. // Here, we create a CameraCaptureSession for camera preview.
cameraDevice.createCaptureSession( cameraDevice.createCaptureSession(
Arrays.asList(surface, imageReader.getSurface(), previewReader.getSurface()), Arrays.asList(surface, previewReader.getSurface()),
new CameraCaptureSession.StateCallback() { new CameraCaptureSession.StateCallback() {
@Override @Override
......
# Description:
# Example MNIST TensorFlow models for demonstrating data reading.
package(default_visibility = ["//tensorflow:internal"])
licenses(["notice"]) # Apache 2.0
exports_files(["LICENSE"])
py_binary(
name = "convert_to_records",
srcs = ["convert_to_records.py"],
srcs_version = "PY2AND3",
deps = [
"//tensorflow:tensorflow_py",
"//tensorflow/examples/tutorials/mnist:input_data",
],
)
py_binary(
name = "fully_connected_reader",
srcs = [
"fully_connected_reader.py",
],
srcs_version = "PY2AND3",
deps = [
"//tensorflow:tensorflow_py",
"//tensorflow/examples/tutorials/mnist",
],
)
py_binary(
name = "fully_connected_preloaded",
srcs = [
"fully_connected_preloaded.py",
],
srcs_version = "PY2AND3",
deps = [
"//tensorflow:tensorflow_py",
"//tensorflow/examples/tutorials/mnist",
"//tensorflow/examples/tutorials/mnist:input_data",
],
)
py_binary(
name = "fully_connected_preloaded_var",
srcs = [
"fully_connected_preloaded_var.py",
],
srcs_version = "PY2AND3",
deps = [
"//tensorflow:tensorflow_py",
"//tensorflow/examples/tutorials/mnist",
"//tensorflow/examples/tutorials/mnist:input_data",
],
)
filegroup(
name = "all_files",
srcs = glob(
["**/*"],
exclude = [
"**/METADATA",
"**/OWNERS",
],
),
visibility = ["//tensorflow:__subpackages__"],
)
...@@ -23,7 +23,7 @@ import tensorflow.python.platform ...@@ -23,7 +23,7 @@ import tensorflow.python.platform
import numpy import numpy
import tensorflow as tf import tensorflow as tf
from tensorflow.g3doc.tutorials.mnist import input_data from tensorflow.examples.tutorials.mnist import input_data
TRAIN_IMAGES = 'train-images-idx3-ubyte.gz' # MNIST filenames TRAIN_IMAGES = 'train-images-idx3-ubyte.gz' # MNIST filenames
...@@ -32,7 +32,7 @@ TEST_IMAGES = 't10k-images-idx3-ubyte.gz' ...@@ -32,7 +32,7 @@ TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
TEST_LABELS = 't10k-labels-idx1-ubyte.gz' TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
tf.app.flags.DEFINE_string('directory', 'data', tf.app.flags.DEFINE_string('directory', '/tmp/data',
'Directory to download data files and write the ' 'Directory to download data files and write the '
'converted result') 'converted result')
tf.app.flags.DEFINE_integer('validation_size', 5000, tf.app.flags.DEFINE_integer('validation_size', 5000,
......
...@@ -15,10 +15,16 @@ ...@@ -15,10 +15,16 @@
"""Trains the MNIST network using preloaded data in a constant. """Trains the MNIST network using preloaded data in a constant.
Command to run this py_binary target: Run using bazel:
bazel run -c opt \ bazel run -c opt \
<...>/tensorflow/g3doc/how_tos/reading_data:fully_connected_preloaded <...>/tensorflow/examples/how_tos/reading_data:fully_connected_preloaded
or, if installed via pip:
cd tensorflow/examples/how_tos/reading_data
python fully_connected_preloaded.py
""" """
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
...@@ -31,8 +37,8 @@ import tensorflow.python.platform ...@@ -31,8 +37,8 @@ import tensorflow.python.platform
import numpy import numpy
import tensorflow as tf import tensorflow as tf
from tensorflow.g3doc.tutorials.mnist import input_data from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.g3doc.tutorials.mnist import mnist from tensorflow.examples.tutorials.mnist import mnist
# Basic model parameters as external flags. # Basic model parameters as external flags.
...@@ -44,7 +50,8 @@ flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.') ...@@ -44,7 +50,8 @@ flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.') flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. ' flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.') 'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.') flags.DEFINE_string('train_dir', '/tmp/data',
'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data ' flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.') 'for unit testing.')
......
...@@ -15,10 +15,15 @@ ...@@ -15,10 +15,15 @@
"""Trains the MNIST network using preloaded data stored in a variable. """Trains the MNIST network using preloaded data stored in a variable.
Command to run this py_binary target: Run using bazel:
bazel run -c opt \ bazel run -c opt \
<...>/tensorflow/g3doc/how_tos/reading_data:fully_connected_preloaded_var <...>/tensorflow/examples/how_tos/reading_data:fully_connected_preloaded_var
or, if installed via pip:
cd tensorflow/examples/how_tos/reading_data
python fully_connected_preloaded_var.py
""" """
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
...@@ -31,8 +36,8 @@ import tensorflow.python.platform ...@@ -31,8 +36,8 @@ import tensorflow.python.platform
import numpy import numpy
import tensorflow as tf import tensorflow as tf
from tensorflow.g3doc.tutorials.mnist import input_data from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.g3doc.tutorials.mnist import mnist from tensorflow.examples.tutorials.mnist import mnist
# Basic model parameters as external flags. # Basic model parameters as external flags.
...@@ -44,7 +49,8 @@ flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.') ...@@ -44,7 +49,8 @@ flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.') flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. ' flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.') 'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.') flags.DEFINE_string('train_dir', '/tmp/data',
'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data ' flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.') 'for unit testing.')
......
...@@ -34,7 +34,7 @@ import tensorflow.python.platform ...@@ -34,7 +34,7 @@ import tensorflow.python.platform
import numpy import numpy
import tensorflow as tf import tensorflow as tf
from tensorflow.g3doc.tutorials.mnist import mnist from tensorflow.examples.tutorials.mnist import mnist
# Basic model parameters as external flags. # Basic model parameters as external flags.
...@@ -45,7 +45,8 @@ flags.DEFINE_integer('num_epochs', 2, 'Number of epochs to run trainer.') ...@@ -45,7 +45,8 @@ flags.DEFINE_integer('num_epochs', 2, 'Number of epochs to run trainer.')
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.') flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.') flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size.') flags.DEFINE_integer('batch_size', 100, 'Batch size.')
flags.DEFINE_string('train_dir', 'data', 'Directory with the training data.') flags.DEFINE_string('train_dir', '/tmp/data',
'Directory with the training data.')
# Constants used for dealing with the files, matches convert_to_records. # Constants used for dealing with the files, matches convert_to_records.
TRAIN_FILE = 'train.tfrecords' TRAIN_FILE = 'train.tfrecords'
......
# Description:
# Example TensorFlow models for MNIST used in tutorials
licenses(["notice"]) # Apache 2.0
exports_files(["LICENSE"])
py_library(
name = "package",
srcs = [
"__init__.py",
],
srcs_version = "PY2AND3",
visibility = ["//tensorflow:__subpackages__"],
deps = [
":input_data",
":mnist",
],
)
py_library(
name = "input_data",
srcs = ["input_data.py"],
srcs_version = "PY2AND3",
visibility = ["//tensorflow:__subpackages__"],
deps = ["//tensorflow:tensorflow_py"],
)
py_library(
name = "mnist",
srcs = [
"mnist.py",
],
srcs_version = "PY2AND3",
visibility = ["//tensorflow:__subpackages__"],
deps = [
"//tensorflow:tensorflow_py",
],
)
py_binary(
name = "fully_connected_feed",
srcs = [
"fully_connected_feed.py",
],
srcs_version = "PY2AND3",
deps = [
":input_data",
":mnist",
"//tensorflow:tensorflow_py",
],
)
py_binary(
name = "mnist_with_summaries",
srcs = [
"mnist_with_summaries.py",
],
srcs_version = "PY2AND3",
deps = [
":input_data",
"//tensorflow:tensorflow_py",
],
)
py_test(
name = "fully_connected_feed_test",
size = "small",
srcs = [
"fully_connected_feed.py",
],
args = [
"--fake_data",
"--max_steps=10",
"--train_dir=/tmp/mnist",
],
main = "fully_connected_feed.py",
srcs_version = "PY2AND3",
deps = [
":input_data",
":mnist",
"//tensorflow:tensorflow_py",
],
)
py_test(
name = "mnist_with_summaries_test",
size = "small",
srcs = [
"mnist_with_summaries.py",
],
args = [
"--fake_data",
"--max_steps=10",
"--learning_rate=0.00",
],
main = "mnist_with_summaries.py",
srcs_version = "PY2AND3",
deps = [
":input_data",
"//tensorflow:tensorflow_py",
],
)
filegroup(
name = "all_files",
srcs = glob(
["**/*"],
exclude = [
"**/METADATA",
"**/OWNERS",
],
),
visibility = ["//tensorflow:__subpackages__"],
)
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Imports mnist tutorial libraries used by tutorial examples."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.examples.tutorials.mnist import mnist
...@@ -13,15 +13,7 @@ ...@@ -13,15 +13,7 @@
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
"""Trains and Evaluates the MNIST network using a feed dictionary. """Trains and Evaluates the MNIST network using a feed dictionary."""
TensorFlow install instructions:
https://tensorflow.org/get_started/os_setup.html
MNIST tutorial:
https://tensorflow.org/tutorials/mnist/tf/index.html
"""
# pylint: disable=missing-docstring # pylint: disable=missing-docstring
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
...@@ -35,8 +27,8 @@ import numpy ...@@ -35,8 +27,8 @@ import numpy
from six.moves import xrange # pylint: disable=redefined-builtin from six.moves import xrange # pylint: disable=redefined-builtin
import tensorflow as tf import tensorflow as tf
from tensorflow.g3doc.tutorials.mnist import input_data from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.g3doc.tutorials.mnist import mnist from tensorflow.examples.tutorials.mnist import mnist
# Basic model parameters as external flags. # Basic model parameters as external flags.
......
...@@ -25,12 +25,6 @@ apply gradients. ...@@ -25,12 +25,6 @@ apply gradients.
This file is used by the various "fully_connected_*.py" files and not meant to This file is used by the various "fully_connected_*.py" files and not meant to
be run. be run.
TensorFlow install instructions:
https://tensorflow.org/get_started/os_setup.html
MNIST tutorial:
https://tensorflow.org/tutorials/mnist/tf/index.html
""" """
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
...@@ -54,14 +48,14 @@ def inference(images, hidden1_units, hidden2_units): ...@@ -54,14 +48,14 @@ def inference(images, hidden1_units, hidden2_units):
Args: Args:
images: Images placeholder, from inputs(). images: Images placeholder, from inputs().
hidden1: Size of the first hidden layer. hidden1_units: Size of the first hidden layer.
hidden2: Size of the second hidden layer. hidden2_units: Size of the second hidden layer.
Returns: Returns:
softmax_linear: Output tensor with the computed logits. softmax_linear: Output tensor with the computed logits.
""" """
# Hidden 1 # Hidden 1
with tf.name_scope('hidden1') as scope: with tf.name_scope('hidden1'):
weights = tf.Variable( weights = tf.Variable(
tf.truncated_normal([IMAGE_PIXELS, hidden1_units], tf.truncated_normal([IMAGE_PIXELS, hidden1_units],
stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),
...@@ -70,7 +64,7 @@ def inference(images, hidden1_units, hidden2_units): ...@@ -70,7 +64,7 @@ def inference(images, hidden1_units, hidden2_units):
name='biases') name='biases')
hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases) hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
# Hidden 2 # Hidden 2
with tf.name_scope('hidden2') as scope: with tf.name_scope('hidden2'):
weights = tf.Variable( weights = tf.Variable(
tf.truncated_normal([hidden1_units, hidden2_units], tf.truncated_normal([hidden1_units, hidden2_units],
stddev=1.0 / math.sqrt(float(hidden1_units))), stddev=1.0 / math.sqrt(float(hidden1_units))),
...@@ -79,7 +73,7 @@ def inference(images, hidden1_units, hidden2_units): ...@@ -79,7 +73,7 @@ def inference(images, hidden1_units, hidden2_units):
name='biases') name='biases')
hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases) hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
# Linear # Linear
with tf.name_scope('softmax_linear') as scope: with tf.name_scope('softmax_linear'):
weights = tf.Variable( weights = tf.Variable(
tf.truncated_normal([hidden2_units, NUM_CLASSES], tf.truncated_normal([hidden2_units, NUM_CLASSES],
stddev=1.0 / math.sqrt(float(hidden2_units))), stddev=1.0 / math.sqrt(float(hidden2_units))),
......
...@@ -23,21 +23,23 @@ from __future__ import division ...@@ -23,21 +23,23 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
# Import data # Import data
import input_data from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
import tensorflow as tf import tensorflow as tf
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
sess = tf.InteractiveSession() sess = tf.InteractiveSession()
# Create the model # Create the model
x = tf.placeholder("float", [None, 784]) x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784,10])) W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10])) b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b) y = tf.nn.softmax(tf.matmul(x, W) + b)
# Define loss and optimizer # Define loss and optimizer
y_ = tf.placeholder("float", [None,10]) y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y)) cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# Train # Train
...@@ -47,6 +49,6 @@ for i in range(1000): ...@@ -47,6 +49,6 @@ for i in range(1000):
train_step.run({x: batch_xs, y_: batch_ys}) train_step.run({x: batch_xs, y_: batch_ys})
# Test trained model # Test trained model
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels})) print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
...@@ -30,7 +30,7 @@ from __future__ import division ...@@ -30,7 +30,7 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
import tensorflow.python.platform import tensorflow.python.platform
from tensorflow.g3doc.tutorials.mnist import input_data from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf import tensorflow as tf
flags = tf.app.flags flags = tf.app.flags
...@@ -54,28 +54,28 @@ def main(_): ...@@ -54,28 +54,28 @@ def main(_):
b = tf.Variable(tf.zeros([10], name='bias')) b = tf.Variable(tf.zeros([10], name='bias'))
# use a name scope to organize nodes in the graph visualizer # use a name scope to organize nodes in the graph visualizer
with tf.name_scope('Wx_b') as scope: with tf.name_scope('Wx_b'):
y = tf.nn.softmax(tf.matmul(x, W) + b) y = tf.nn.softmax(tf.matmul(x, W) + b)
# Add summary ops to collect data # Add summary ops to collect data
w_hist = tf.histogram_summary('weights', W) _ = tf.histogram_summary('weights', W)
b_hist = tf.histogram_summary('biases', b) _ = tf.histogram_summary('biases', b)
y_hist = tf.histogram_summary('y', y) _ = tf.histogram_summary('y', y)
# Define loss and optimizer # Define loss and optimizer
y_ = tf.placeholder('float', [None, 10], name='y-input') y_ = tf.placeholder('float', [None, 10], name='y-input')
# More name scopes will clean up the graph representation # More name scopes will clean up the graph representation
with tf.name_scope('xent') as scope: with tf.name_scope('xent'):
cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
ce_summ = tf.scalar_summary('cross entropy', cross_entropy) _ = tf.scalar_summary('cross entropy', cross_entropy)
with tf.name_scope('train') as scope: with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer( train_step = tf.train.GradientDescentOptimizer(
FLAGS.learning_rate).minimize(cross_entropy) FLAGS.learning_rate).minimize(cross_entropy)
with tf.name_scope('test') as scope: with tf.name_scope('test'):
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
accuracy_summary = tf.scalar_summary('accuracy', accuracy) _ = tf.scalar_summary('accuracy', accuracy)
# Merge all the summaries and write them out to /tmp/mnist_logs # Merge all the summaries and write them out to /tmp/mnist_logs
merged = tf.merge_all_summaries() merged = tf.merge_all_summaries()
......
# Description:
# TensorFlow model for word2vec
package(default_visibility = ["//tensorflow:internal"])
licenses(["notice"]) # Apache 2.0
exports_files(["LICENSE"])
py_binary(
name = "word2vec_basic",
srcs = [
"word2vec_basic.py",
],
srcs_version = "PY2AND3",
deps = [
"//tensorflow:tensorflow_py",
],
)
filegroup(
name = "all_files",
srcs = glob(
["**/*"],
exclude = [
"**/METADATA",
"**/OWNERS",
],
),
)
...@@ -35,7 +35,7 @@ it is executed without a feed, so you won't forget to feed it. ...@@ -35,7 +35,7 @@ it is executed without a feed, so you won't forget to feed it.
An example using `placeholder` and feeding to train on MNIST data can be found An example using `placeholder` and feeding to train on MNIST data can be found
in in
[`tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py), [`tensorflow/examples/tutorials/mnist/fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/fully_connected_feed.py),
and is described in the [MNIST tutorial](../../tutorials/mnist/tf/index.md). and is described in the [MNIST tutorial](../../tutorials/mnist/tf/index.md).
## Reading from files ## Reading from files
...@@ -154,7 +154,7 @@ as a field). You write a little program that gets your data, stuffs it in an ...@@ -154,7 +154,7 @@ as a field). You write a little program that gets your data, stuffs it in an
writes the string to a TFRecords file using the writes the string to a TFRecords file using the
[`tf.python_io.TFRecordWriter` class](../../api_docs/python/python_io.md#TFRecordWriter). [`tf.python_io.TFRecordWriter` class](../../api_docs/python/python_io.md#TFRecordWriter).
For example, For example,
[`tensorflow/g3doc/how_tos/reading_data/convert_to_records.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/convert_to_records.py) [`tensorflow/examples/how_tos/reading_data/convert_to_records.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/how_tos/reading_data/convert_to_records.py)
converts MNIST data to this format. converts MNIST data to this format.
To read a file of TFRecords, use To read a file of TFRecords, use
...@@ -163,7 +163,7 @@ the [`tf.parse_single_example`](../../api_docs/python/io_ops.md#parse_single_exa ...@@ -163,7 +163,7 @@ the [`tf.parse_single_example`](../../api_docs/python/io_ops.md#parse_single_exa
decoder. The `parse_single_example` op decodes the example protocol buffers into decoder. The `parse_single_example` op decodes the example protocol buffers into
tensors. An MNIST example using the data produced by `convert_to_records` can be tensors. An MNIST example using the data produced by `convert_to_records` can be
found in found in
[`tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py), [`tensorflow/examples/how_tos/reading_data/fully_connected_reader.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py),
which you can compare with the `fully_connected_feed` version. which you can compare with the `fully_connected_feed` version.
### Preprocessing ### Preprocessing
...@@ -455,8 +455,8 @@ multiple preprocessing threads, set the `num_threads` parameter to a number ...@@ -455,8 +455,8 @@ multiple preprocessing threads, set the `num_threads` parameter to a number
bigger than 1. bigger than 1.
An MNIST example that preloads the data using constants can be found in An MNIST example that preloads the data using constants can be found in
[`tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py), and one that preloads the data using variables can be found in [`tensorflow/examples/how_tos/reading_data/fully_connected_preloaded.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/how_tos/reading_data/fully_connected_preloaded.py), and one that preloads the data using variables can be found in
[`tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py), [`tensorflow/examples/how_tos/reading_data/fully_connected_preloaded_var.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/how_tos/reading_data/fully_connected_preloaded_var.py),
You can compare these with the `fully_connected_feed` and You can compare these with the `fully_connected_feed` and
`fully_connected_reader` versions above. `fully_connected_reader` versions above.
......
...@@ -255,6 +255,11 @@ these summaries to a log directory. Then, start TensorBoard using ...@@ -255,6 +255,11 @@ these summaries to a log directory. Then, start TensorBoard using
For more details, see the [Summaries and TensorBoard tutorial] For more details, see the [Summaries and TensorBoard tutorial]
(../how_tos/summaries_and_tensorboard/index.md). (../how_tos/summaries_and_tensorboard/index.md).
#### Every time I launch TensorBoard, I get a network security popup!
You can change TensorBoard to serve on localhost rather than '0.0.0.0' by
the flag --host=localhost. This should quiet any security warnings.
## Extending TensorFlow ## Extending TensorFlow
See also the how-to documentation for See also the how-to documentation for
......
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
*This tutorial is intended for readers who are new to both machine learning and *This tutorial is intended for readers who are new to both machine learning and
TensorFlow. If you already TensorFlow. If you already
know what MNIST is, and what softmax (multinomial logistic) regression is, know what MNIST is, and what softmax (multinomial logistic) regression is,
you might prefer this [faster paced tutorial](../pros/index.md).* you might prefer this [faster paced tutorial](../pros/index.md).
Be sure to [install TensorFlow](../../../get_started/os_setup.md) before
starting either tutorial.*
When one learns how to program, there's a tradition that the first thing you do When one learns how to program, there's a tradition that the first thing you do
is print "Hello World." Just like programming has Hello World, machine learning is print "Hello World." Just like programming has Hello World, machine learning
...@@ -37,11 +39,11 @@ The MNIST data is hosted on ...@@ -37,11 +39,11 @@ The MNIST data is hosted on
[Yann LeCun's website](http://yann.lecun.com/exdb/mnist/). For your [Yann LeCun's website](http://yann.lecun.com/exdb/mnist/). For your
convenience, we've included some python code to download and install the data convenience, we've included some python code to download and install the data
automatically. You can either download automatically. You can either download
[the code](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py) [the code](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py)
and import it as below, or simply copy and paste it in. and import it as below, or simply copy and paste it in.
```python ```python
import input_data import tensorflow.examples.tutorials.mnist.input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
``` ```
......
# MNIST Data Download # MNIST Data Download
Code: [tensorflow/g3doc/tutorials/mnist/](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/) Code: [tensorflow/examples/tutorials/mnist/](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/)
The goal of this tutorial is to show how to download the dataset files required The goal of this tutorial is to show how to download the dataset files required
for handwritten digit classification using the (classic) MNIST data set. for handwritten digit classification using the (classic) MNIST data set.
...@@ -11,7 +11,7 @@ This tutorial references the following files: ...@@ -11,7 +11,7 @@ This tutorial references the following files:
File | Purpose File | Purpose
--- | --- --- | ---
[`input_data.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py) | The code to download the MNIST dataset for training and evaluation. [`input_data.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py) | The code to download the MNIST dataset for training and evaluation.
## Prepare the Data ## Prepare the Data
......
...@@ -9,7 +9,8 @@ while constructing a deep convolutional MNIST classifier. ...@@ -9,7 +9,8 @@ while constructing a deep convolutional MNIST classifier.
*This introduction assumes familiarity with neural networks and the MNIST *This introduction assumes familiarity with neural networks and the MNIST
dataset. If you don't have dataset. If you don't have
a background with them, check out the a background with them, check out the
[introduction for beginners](../beginners/index.md).* [introduction for beginners](../beginners/index.md). Be sure to
[install TensorFlow](../../../get_started/os_setup.md) before starting.*
## Setup ## Setup
...@@ -19,7 +20,7 @@ TensorFlow session. ...@@ -19,7 +20,7 @@ TensorFlow session.
### Load MNIST Data ### Load MNIST Data
For your convenience, we've included For your convenience, we've included
[a script](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py) [a script](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py)
which automatically downloads and imports the MNIST dataset. It will create a which automatically downloads and imports the MNIST dataset. It will create a
directory `'MNIST_data'` in which to store the data files. directory `'MNIST_data'` in which to store the data files.
......
# TensorFlow Mechanics 101 # TensorFlow Mechanics 101
Code: [tensorflow/g3doc/tutorials/mnist/](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/) Code: [tensorflow/examples/tutorials/mnist/](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/)
The goal of this tutorial is to show how to use TensorFlow to train and The goal of this tutorial is to show how to use TensorFlow to train and
evaluate a simple feed-forward neural network for handwritten digit evaluate a simple feed-forward neural network for handwritten digit
...@@ -18,8 +18,8 @@ This tutorial references the following files: ...@@ -18,8 +18,8 @@ This tutorial references the following files:
File | Purpose File | Purpose
--- | --- --- | ---
[`mnist.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/mnist.py) | The code to build a fully-connected MNIST model. [`mnist.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/mnist.py) | The code to build a fully-connected MNIST model.
[`fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py) | The main code to train the built MNIST model against the downloaded dataset using a feed dictionary. [`fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/fully_connected_feed.py) | The main code to train the built MNIST model against the downloaded dataset using a feed dictionary.
Simply run the `fully_connected_feed.py` file directly to start training: Simply run the `fully_connected_feed.py` file directly to start training:
......
...@@ -19,7 +19,7 @@ represent words as vectors. ...@@ -19,7 +19,7 @@ represent words as vectors.
We walk through the code later during the tutorial, but if you'd prefer to dive We walk through the code later during the tutorial, but if you'd prefer to dive
straight in, feel free to look at the minimalistic implementation in straight in, feel free to look at the minimalistic implementation in
[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py) [tensorflow/examples/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/word2vec/word2vec_basic.py)
This basic example contains the code needed to download some data, train on it a This basic example contains the code needed to download some data, train on it a
bit and visualize the result. Once you get comfortable with reading and running bit and visualize the result. Once you get comfortable with reading and running
the basic version, you can graduate to the basic version, you can graduate to
...@@ -269,7 +269,7 @@ nce_biases = tf.Variable(tf.zeros([vocabulary_size])) ...@@ -269,7 +269,7 @@ nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
Now that we have the parameters in place, we can define our skip-gram model Now that we have the parameters in place, we can define our skip-gram model
graph. For simplicity, let's suppose we've already integerized our text corpus graph. For simplicity, let's suppose we've already integerized our text corpus
with a vocabulary so that each word is represented as an integer (see with a vocabulary so that each word is represented as an integer (see
[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py) [tensorflow/examples/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/word2vec/word2vec_basic.py)
for the details). The skip-gram model takes two inputs. One is a batch full of for the details). The skip-gram model takes two inputs. One is a batch full of
integers representing the source context words, the other is for the target integers representing the source context words, the other is for the target
words. Let's create placeholder nodes for these inputs, so that we can feed in words. Let's create placeholder nodes for these inputs, so that we can feed in
...@@ -321,7 +321,7 @@ for inputs, labels in generate_batch(...): ...@@ -321,7 +321,7 @@ for inputs, labels in generate_batch(...):
``` ```
See the full example code in See the full example code in
[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](./word2vec_basic.py). [tensorflow/examples/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/word2vec/word2vec_basic.py).
## Visualizing the Learned Embeddings ## Visualizing the Learned Embeddings
......
...@@ -59,8 +59,10 @@ class DType(object): ...@@ -59,8 +59,10 @@ class DType(object):
@@base_dtype @@base_dtype
@@is_ref_dtype @@is_ref_dtype
@@as_ref @@as_ref
@@is_floating
@@is_integer @@is_integer
@@is_quantized @@is_quantized
@@is_unsigned
@@as_numpy_dtype @@as_numpy_dtype
@@as_datatype_enum @@as_datatype_enum
...@@ -136,6 +138,21 @@ class DType(object): ...@@ -136,6 +138,21 @@ class DType(object):
"""Returns whether this is a quantized data type.""" """Returns whether this is a quantized data type."""
return self.base_dtype in [qint8, quint8, qint32, bfloat16] return self.base_dtype in [qint8, quint8, qint32, bfloat16]
@property
def is_unsigned(self):
"""Returns whether this type is unsigned.
Non-numeric, unordered, and quantized types are not considered unsigned, and
this function returns `False`.
Returns:
Whether a `DType` is unsigned.
"""
try:
return self.min == 0
except TypeError:
return False
@property @property
def min(self): def min(self):
"""Returns the minimum representable value in this data type. """Returns the minimum representable value in this data type.
......
...@@ -145,6 +145,18 @@ class TypesTest(test_util.TensorFlowTestCase): ...@@ -145,6 +145,18 @@ class TypesTest(test_util.TensorFlowTestCase):
self.assertEqual(tf.as_dtype("string").is_floating, False) self.assertEqual(tf.as_dtype("string").is_floating, False)
self.assertEqual(tf.as_dtype("bool").is_floating, False) self.assertEqual(tf.as_dtype("bool").is_floating, False)
def testIsUnsigned(self):
self.assertEqual(tf.as_dtype("int8").is_unsigned, False)
self.assertEqual(tf.as_dtype("int16").is_unsigned, False)
self.assertEqual(tf.as_dtype("int32").is_unsigned, False)
self.assertEqual(tf.as_dtype("int64").is_unsigned, False)
self.assertEqual(tf.as_dtype("uint8").is_unsigned, True)
self.assertEqual(tf.as_dtype("float32").is_unsigned, False)
self.assertEqual(tf.as_dtype("float64").is_unsigned, False)
self.assertEqual(tf.as_dtype("bool").is_unsigned, False)
self.assertEqual(tf.as_dtype("string").is_unsigned, False)
self.assertEqual(tf.as_dtype("complex64").is_unsigned, False)
def testMinMax(self): def testMinMax(self):
# make sure min/max evaluates for all data types that have min/max # make sure min/max evaluates for all data types that have min/max
for datatype_enum in types_pb2.DataType.values(): for datatype_enum in types_pb2.DataType.values():
......
...@@ -106,6 +106,11 @@ class ReshapeTest(tf.test.TestCase): ...@@ -106,6 +106,11 @@ class ReshapeTest(tf.test.TestCase):
with self.assertRaisesRegexp(ValueError, "isn't divisible by 17"): with self.assertRaisesRegexp(ValueError, "isn't divisible by 17"):
tf.reshape(y, [17, -1]) tf.reshape(y, [17, -1])
z = tf.constant(0.0, shape=[32, 128])
with self.assertRaisesRegexp(ValueError,
"Cannot reshape a tensor with 4096 elements"):
tf.reshape(z, [4095])
def testPartialShapes(self): def testPartialShapes(self):
x = tf.placeholder(tf.float32) x = tf.placeholder(tf.float32)
......
...@@ -64,7 +64,10 @@ class RNNTest(tf.test.TestCase): ...@@ -64,7 +64,10 @@ class RNNTest(tf.test.TestCase):
def testRNN(self): def testRNN(self):
cell = Plus1RNNCell() cell = Plus1RNNCell()
batch_size = 2 batch_size = 2
inputs = [tf.placeholder(tf.float32, shape=(batch_size, 5))] * 10 input_size = 5
max_length = 8 # unrolled up to this length
inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))]
outputs, states = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs, states = tf.nn.rnn(cell, inputs, dtype=tf.float32)
self.assertEqual(len(outputs), len(inputs)) self.assertEqual(len(outputs), len(inputs))
for out, inp in zip(outputs, inputs): for out, inp in zip(outputs, inputs):
...@@ -72,7 +75,7 @@ class RNNTest(tf.test.TestCase): ...@@ -72,7 +75,7 @@ class RNNTest(tf.test.TestCase):
self.assertEqual(out.dtype, inp.dtype) self.assertEqual(out.dtype, inp.dtype)
with self.test_session(use_gpu=False) as sess: with self.test_session(use_gpu=False) as sess:
input_value = np.random.randn(batch_size, 5) input_value = np.random.randn(batch_size, input_size)
values = sess.run(outputs + [states[-1]], values = sess.run(outputs + [states[-1]],
feed_dict={inputs[0]: input_value}) feed_dict={inputs[0]: input_value})
...@@ -82,14 +85,18 @@ class RNNTest(tf.test.TestCase): ...@@ -82,14 +85,18 @@ class RNNTest(tf.test.TestCase):
# Final state # Final state
self.assertAllClose( self.assertAllClose(
values[-1], 10.0*np.ones((batch_size, 5), dtype=np.float32)) values[-1],
max_length * np.ones((batch_size, input_size), dtype=np.float32))
def testDropout(self): def testDropout(self):
cell = Plus1RNNCell() cell = Plus1RNNCell()
full_dropout_cell = tf.nn.rnn_cell.DropoutWrapper( full_dropout_cell = tf.nn.rnn_cell.DropoutWrapper(
cell, input_keep_prob=1e-12, seed=0) cell, input_keep_prob=1e-12, seed=0)
batch_size = 2 batch_size = 2
inputs = [tf.placeholder(tf.float32, shape=(batch_size, 5))] * 10 input_size = 5
max_length = 8
inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))]
with tf.variable_scope("share_scope"): with tf.variable_scope("share_scope"):
outputs, states = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs, states = tf.nn.rnn(cell, inputs, dtype=tf.float32)
with tf.variable_scope("drop_scope"): with tf.variable_scope("drop_scope"):
...@@ -101,7 +108,7 @@ class RNNTest(tf.test.TestCase): ...@@ -101,7 +108,7 @@ class RNNTest(tf.test.TestCase):
self.assertEqual(out.dtype, inp.dtype) self.assertEqual(out.dtype, inp.dtype)
with self.test_session(use_gpu=False) as sess: with self.test_session(use_gpu=False) as sess:
input_value = np.random.randn(batch_size, 5) input_value = np.random.randn(batch_size, input_size)
values = sess.run(outputs + [states[-1]], values = sess.run(outputs + [states[-1]],
feed_dict={inputs[0]: input_value}) feed_dict={inputs[0]: input_value})
full_dropout_values = sess.run(dropped_outputs, full_dropout_values = sess.run(dropped_outputs,
...@@ -116,7 +123,10 @@ class RNNTest(tf.test.TestCase): ...@@ -116,7 +123,10 @@ class RNNTest(tf.test.TestCase):
cell = Plus1RNNCell() cell = Plus1RNNCell()
sequence_length = tf.placeholder(tf.int64) sequence_length = tf.placeholder(tf.int64)
batch_size = 2 batch_size = 2
inputs = [tf.placeholder(tf.float32, shape=(batch_size, 5))] * 10 input_size = 5
max_length = 8
inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))]
with tf.variable_scope("drop_scope"): with tf.variable_scope("drop_scope"):
dynamic_outputs, dynamic_states = tf.nn.rnn( dynamic_outputs, dynamic_states = tf.nn.rnn(
cell, inputs, sequence_length=sequence_length, dtype=tf.float32) cell, inputs, sequence_length=sequence_length, dtype=tf.float32)
...@@ -124,7 +134,7 @@ class RNNTest(tf.test.TestCase): ...@@ -124,7 +134,7 @@ class RNNTest(tf.test.TestCase):
self.assertEqual(len(dynamic_states), len(inputs)) self.assertEqual(len(dynamic_states), len(inputs))
with self.test_session(use_gpu=False) as sess: with self.test_session(use_gpu=False) as sess:
input_value = np.random.randn(batch_size, 5) input_value = np.random.randn(batch_size, input_size)
dynamic_values = sess.run(dynamic_outputs, dynamic_values = sess.run(dynamic_outputs,
feed_dict={inputs[0]: input_value, feed_dict={inputs[0]: input_value,
sequence_length: [2, 3]}) sequence_length: [2, 3]})
...@@ -136,7 +146,8 @@ class RNNTest(tf.test.TestCase): ...@@ -136,7 +146,8 @@ class RNNTest(tf.test.TestCase):
for v in dynamic_values[:3]: for v in dynamic_values[:3]:
self.assertAllClose(v, input_value + 1.0) self.assertAllClose(v, input_value + 1.0)
for vi, v in enumerate(dynamic_state_values[:3]): for vi, v in enumerate(dynamic_state_values[:3]):
self.assertAllEqual(v, 1.0 * (vi + 1) * np.ones((batch_size, 5))) self.assertAllEqual(v, 1.0 * (vi + 1) *
np.ones((batch_size, input_size)))
# zeros for t = 3+ # zeros for t = 3+
for v in dynamic_values[3:]: for v in dynamic_values[3:]:
self.assertAllEqual(v, np.zeros_like(input_value)) self.assertAllEqual(v, np.zeros_like(input_value))
...@@ -154,11 +165,12 @@ class LSTMTest(tf.test.TestCase): ...@@ -154,11 +165,12 @@ class LSTMTest(tf.test.TestCase):
num_units = 3 num_units = 3
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, initializer=initializer) num_units, input_size, initializer=initializer)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))] tf.placeholder(tf.float32, shape=(batch_size, input_size))]
outputs, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32)
self.assertEqual(len(outputs), len(inputs)) self.assertEqual(len(outputs), len(inputs))
...@@ -173,12 +185,13 @@ class LSTMTest(tf.test.TestCase): ...@@ -173,12 +185,13 @@ class LSTMTest(tf.test.TestCase):
num_units = 3 num_units = 3
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, use_peepholes=True, num_units, input_size, use_peepholes=True,
cell_clip=0.0, initializer=initializer) cell_clip=0.0, initializer=initializer)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))] tf.placeholder(tf.float32, shape=(batch_size, input_size))]
outputs, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32)
self.assertEqual(len(outputs), len(inputs)) self.assertEqual(len(outputs), len(inputs))
...@@ -197,12 +210,13 @@ class LSTMTest(tf.test.TestCase): ...@@ -197,12 +210,13 @@ class LSTMTest(tf.test.TestCase):
num_units = 3 num_units = 3
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
state_saver = TestStateSaver(batch_size, 2*num_units) state_saver = TestStateSaver(batch_size, 2 * num_units)
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, use_peepholes=False, initializer=initializer) num_units, input_size, use_peepholes=False, initializer=initializer)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))] tf.placeholder(tf.float32, shape=(batch_size, input_size))]
with tf.variable_scope("share_scope"): with tf.variable_scope("share_scope"):
outputs, states = tf.nn.state_saving_rnn( outputs, states = tf.nn.state_saving_rnn(
...@@ -223,9 +237,10 @@ class LSTMTest(tf.test.TestCase): ...@@ -223,9 +237,10 @@ class LSTMTest(tf.test.TestCase):
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
num_proj = 4 num_proj = 4
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(None, input_size))] tf.placeholder(tf.float32, shape=(None, input_size))]
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, use_peepholes=True, num_units, input_size, use_peepholes=True,
...@@ -244,10 +259,11 @@ class LSTMTest(tf.test.TestCase): ...@@ -244,10 +259,11 @@ class LSTMTest(tf.test.TestCase):
num_proj = 4 num_proj = 4
num_proj_shards = 4 num_proj_shards = 4
num_unit_shards = 2 num_unit_shards = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(None, input_size))] tf.placeholder(tf.float32, shape=(None, input_size))]
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
...@@ -274,9 +290,10 @@ class LSTMTest(tf.test.TestCase): ...@@ -274,9 +290,10 @@ class LSTMTest(tf.test.TestCase):
num_proj = 4 num_proj = 4
num_proj_shards = 4 num_proj_shards = 4
num_unit_shards = 2 num_unit_shards = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-1, 1, seed=self._seed) initializer = tf.random_uniform_initializer(-1, 1, seed=self._seed)
inputs = 10 * [tf.placeholder(tf.float64)] inputs = max_length * [tf.placeholder(tf.float64)]
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, num_units,
...@@ -305,8 +322,9 @@ class LSTMTest(tf.test.TestCase): ...@@ -305,8 +322,9 @@ class LSTMTest(tf.test.TestCase):
num_proj = 4 num_proj = 4
num_proj_shards = 4 num_proj_shards = 4
num_unit_shards = 2 num_unit_shards = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
inputs = 10 * [tf.placeholder(tf.float32)] inputs = max_length * [tf.placeholder(tf.float32)]
initializer = tf.constant_initializer(0.001) initializer = tf.constant_initializer(0.001)
cell_noshard = tf.nn.rnn_cell.LSTMCell( cell_noshard = tf.nn.rnn_cell.LSTMCell(
...@@ -355,10 +373,11 @@ class LSTMTest(tf.test.TestCase): ...@@ -355,10 +373,11 @@ class LSTMTest(tf.test.TestCase):
num_proj = 4 num_proj = 4
num_proj_shards = 4 num_proj_shards = 4
num_unit_shards = 2 num_unit_shards = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
sequence_length = tf.placeholder(tf.int64) sequence_length = tf.placeholder(tf.int64)
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
inputs = 10 * [tf.placeholder(tf.float64)] inputs = max_length * [tf.placeholder(tf.float64)]
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, num_units,
...@@ -392,28 +411,33 @@ class LSTMTest(tf.test.TestCase): ...@@ -392,28 +411,33 @@ class LSTMTest(tf.test.TestCase):
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
num_proj = 4 num_proj = 4
max_length = 8
with self.test_session(graph=tf.Graph()) as sess: with self.test_session(graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-1, 1, seed=self._seed) initializer = tf.random_uniform_initializer(-1, 1, seed=self._seed)
inputs = 10 * [ initializer_d = tf.random_uniform_initializer(-1, 1, seed=self._seed+1)
inputs = max_length * [
tf.placeholder(tf.float32, shape=(None, input_size))] tf.placeholder(tf.float32, shape=(None, input_size))]
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, use_peepholes=True, num_units, input_size, use_peepholes=True,
num_proj=num_proj, initializer=initializer) num_proj=num_proj, initializer=initializer)
cell_d = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, use_peepholes=True,
num_proj=num_proj, initializer=initializer_d)
with tf.variable_scope("share_scope"): with tf.variable_scope("share_scope"):
outputs0, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs0, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32)
with tf.variable_scope("share_scope", reuse=True): with tf.variable_scope("share_scope", reuse=True):
outputs1, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs1, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32)
with tf.variable_scope("diff_scope"): with tf.variable_scope("diff_scope"):
outputs2, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32) outputs2, _ = tf.nn.rnn(cell_d, inputs, dtype=tf.float32)
tf.initialize_all_variables().run() tf.initialize_all_variables().run()
input_value = np.random.randn(batch_size, input_size) input_value = np.random.randn(batch_size, input_size)
output_values = sess.run( output_values = sess.run(
outputs0 + outputs1 + outputs2, feed_dict={inputs[0]: input_value}) outputs0 + outputs1 + outputs2, feed_dict={inputs[0]: input_value})
outputs0_values = output_values[:10] outputs0_values = output_values[:max_length]
outputs1_values = output_values[10:20] outputs1_values = output_values[max_length:2*max_length]
outputs2_values = output_values[20:] outputs2_values = output_values[2*max_length:]
self.assertEqual(len(outputs0_values), len(outputs1_values)) self.assertEqual(len(outputs0_values), len(outputs1_values))
self.assertEqual(len(outputs0_values), len(outputs2_values)) self.assertEqual(len(outputs0_values), len(outputs2_values))
for o1, o2, o3 in zip(outputs0_values, outputs1_values, outputs2_values): for o1, o2, o3 in zip(outputs0_values, outputs1_values, outputs2_values):
...@@ -427,9 +451,10 @@ class LSTMTest(tf.test.TestCase): ...@@ -427,9 +451,10 @@ class LSTMTest(tf.test.TestCase):
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
num_proj = 4 num_proj = 4
max_length = 8
with self.test_session(graph=tf.Graph()) as sess: with self.test_session(graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-1, 1, seed=self._seed) initializer = tf.random_uniform_initializer(-1, 1, seed=self._seed)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(None, input_size))] tf.placeholder(tf.float32, shape=(None, input_size))]
cell = tf.nn.rnn_cell.LSTMCell( cell = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, use_peepholes=True, num_units, input_size, use_peepholes=True,
...@@ -446,43 +471,43 @@ class LSTMTest(tf.test.TestCase): ...@@ -446,43 +471,43 @@ class LSTMTest(tf.test.TestCase):
input_value = np.random.randn(batch_size, input_size) input_value = np.random.randn(batch_size, input_size)
output_values = sess.run( output_values = sess.run(
outputs0 + outputs1, feed_dict={inputs[0]: input_value}) outputs0 + outputs1, feed_dict={inputs[0]: input_value})
outputs0_values = output_values[:10] outputs0_values = output_values[:max_length]
outputs1_values = output_values[10:] outputs1_values = output_values[max_length:]
self.assertEqual(len(outputs0_values), len(outputs1_values)) self.assertEqual(len(outputs0_values), len(outputs1_values))
for out0, out1 in zip(outputs0_values, outputs1_values): for out0, out1 in zip(outputs0_values, outputs1_values):
self.assertAllEqual(out0, out1) self.assertAllEqual(out0, out1)
def testNoProjNoShardingSimpleStateSaver(self): def testNoProjNoShardingSimpleStateSaver(self):
self._testNoProjNoShardingSimpleStateSaver(False) self._testNoProjNoShardingSimpleStateSaver(use_gpu=False)
self._testNoProjNoShardingSimpleStateSaver(True) self._testNoProjNoShardingSimpleStateSaver(use_gpu=True)
def testNoProjNoSharding(self): def testNoProjNoSharding(self):
self._testNoProjNoSharding(False) self._testNoProjNoSharding(use_gpu=False)
self._testNoProjNoSharding(True) self._testNoProjNoSharding(use_gpu=True)
def testCellClipping(self): def testCellClipping(self):
self._testCellClipping(False) self._testCellClipping(use_gpu=False)
self._testCellClipping(True) self._testCellClipping(use_gpu=True)
def testProjNoSharding(self): def testProjNoSharding(self):
self._testProjNoSharding(False) self._testProjNoSharding(use_gpu=False)
self._testProjNoSharding(True) self._testProjNoSharding(use_gpu=True)
def testProjSharding(self): def testProjSharding(self):
self._testProjSharding(False) self._testProjSharding(use_gpu=False)
self._testProjSharding(True) self._testProjSharding(use_gpu=True)
def testShardNoShardEquivalentOutput(self): def testShardNoShardEquivalentOutput(self):
self._testShardNoShardEquivalentOutput(False) self._testShardNoShardEquivalentOutput(use_gpu=False)
self._testShardNoShardEquivalentOutput(True) self._testShardNoShardEquivalentOutput(use_gpu=True)
def testDoubleInput(self): def testDoubleInput(self):
self._testDoubleInput(False) self._testDoubleInput(use_gpu=False)
self._testDoubleInput(True) self._testDoubleInput(use_gpu=True)
def testDoubleInputWithDropoutAndDynamicCalculation(self): def testDoubleInputWithDropoutAndDynamicCalculation(self):
self._testDoubleInputWithDropoutAndDynamicCalculation(False) self._testDoubleInputWithDropoutAndDynamicCalculation(use_gpu=False)
self._testDoubleInputWithDropoutAndDynamicCalculation(True) self._testDoubleInputWithDropoutAndDynamicCalculation(use_gpu=True)
class BidirectionalRNNTest(tf.test.TestCase): class BidirectionalRNNTest(tf.test.TestCase):
...@@ -495,6 +520,7 @@ class BidirectionalRNNTest(tf.test.TestCase): ...@@ -495,6 +520,7 @@ class BidirectionalRNNTest(tf.test.TestCase):
num_units = 3 num_units = 3
input_size = 5 input_size = 5
batch_size = 2 batch_size = 2
max_length = 8
with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess: with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed) initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=self._seed)
sequence_length = tf.placeholder(tf.int64) sequence_length = tf.placeholder(tf.int64)
...@@ -502,7 +528,7 @@ class BidirectionalRNNTest(tf.test.TestCase): ...@@ -502,7 +528,7 @@ class BidirectionalRNNTest(tf.test.TestCase):
num_units, input_size, initializer=initializer) num_units, input_size, initializer=initializer)
cell_bw = tf.nn.rnn_cell.LSTMCell( cell_bw = tf.nn.rnn_cell.LSTMCell(
num_units, input_size, initializer=initializer) num_units, input_size, initializer=initializer)
inputs = 10 * [ inputs = max_length * [
tf.placeholder(tf.float32, shape=(batch_size, input_size))] tf.placeholder(tf.float32, shape=(batch_size, input_size))]
outputs = tf.nn.bidirectional_rnn( outputs = tf.nn.bidirectional_rnn(
cell_fw, cell_bw, inputs, dtype=tf.float32, cell_fw, cell_bw, inputs, dtype=tf.float32,
......
...@@ -844,6 +844,12 @@ def _SqueezeShape(op): ...@@ -844,6 +844,12 @@ def _SqueezeShape(op):
def _ReshapeShape(op): def _ReshapeShape(op):
"""Shape function for Reshape op.""" """Shape function for Reshape op."""
input_shape = op.inputs[0].get_shape() input_shape = op.inputs[0].get_shape()
if input_shape.ndims is not None:
num_elements = tensor_shape.Dimension(1)
for dim in input_shape.dims:
num_elements *= dim
else:
num_elements = tensor_shape.Dimension(None)
new_shape_shape = op.inputs[1].get_shape().with_rank_at_most(1) new_shape_shape = op.inputs[1].get_shape().with_rank_at_most(1)
new_shape = tensor_util.ConstantValue(op.inputs[1]) new_shape = tensor_util.ConstantValue(op.inputs[1])
if new_shape is None: if new_shape is None:
...@@ -853,13 +859,15 @@ def _ReshapeShape(op): ...@@ -853,13 +859,15 @@ def _ReshapeShape(op):
new_shape = np.reshape(new_shape, -1).tolist() new_shape = np.reshape(new_shape, -1).tolist()
if -1 not in new_shape: if -1 not in new_shape:
# The new shape is fully defined. # The new shape is fully defined.
if (num_elements.value is not None
and num_elements.value != np.prod(new_shape)):
raise ValueError(
"Cannot reshape a tensor with %d elements to shape %s (%d elements)"
% (num_elements.value, new_shape, np.prod(new_shape)))
return [tensor_shape.TensorShape(new_shape)] return [tensor_shape.TensorShape(new_shape)]
elif input_shape.is_fully_defined(): elif num_elements.value is not None:
# We know the input shape, so we can calculate the missing # We know the number of elements, so we can calculate the missing
# dimension in the new_shape. # dimension in the new_shape.
num_elements = 1
for dim in input_shape.dims:
num_elements *= dim.value
known_elements = 1 known_elements = 1
unknown_index = None unknown_index = None
for i, dim in enumerate(new_shape): for i, dim in enumerate(new_shape):
......
...@@ -52,11 +52,8 @@ def clip_by_value(t, clip_value_min, clip_value_max, ...@@ -52,11 +52,8 @@ def clip_by_value(t, clip_value_min, clip_value_max,
t = ops.convert_to_tensor(t, name="t") t = ops.convert_to_tensor(t, name="t")
# Go through list of tensors, for each value in each tensor clip # Go through list of tensors, for each value in each tensor clip
t_min = math_ops.minimum( t_min = math_ops.minimum(t, clip_value_max)
t, array_ops.fill(array_ops.shape(t), clip_value_max)) t_max = math_ops.maximum(t_min, clip_value_min, name=name)
t_max = math_ops.maximum(
t_min, array_ops.fill(array_ops.shape(t), clip_value_min),
name=name)
return t_max return t_max
......
...@@ -444,7 +444,7 @@ def gradients(ys, ...@@ -444,7 +444,7 @@ def gradients(ys,
op_wrapper = control_flow_ops.MakeWrapper(op) op_wrapper = control_flow_ops.MakeWrapper(op)
in_grads = _AsList(grad_fn(op_wrapper, *out_grads)) in_grads = _AsList(grad_fn(op_wrapper, *out_grads))
_VerifyGeneratedGradients(in_grads, op) _VerifyGeneratedGradients(in_grads, op)
if gate_gradients and len(in_grads) > 1: if gate_gradients and len(filter(None, in_grads)) > 1:
in_grads = control_flow_ops.tuple(in_grads) in_grads = control_flow_ops.tuple(in_grads)
logging.vlog(1, "Gradient for '" + op.name + "'") logging.vlog(1, "Gradient for '" + op.name + "'")
logging.vlog(1, " in --> %s", logging.vlog(1, " in --> %s",
......
...@@ -55,7 +55,8 @@ def rnn(cell, inputs, initial_state=None, dtype=None, ...@@ -55,7 +55,8 @@ def rnn(cell, inputs, initial_state=None, dtype=None,
Args: Args:
cell: An instance of RNNCell. cell: An instance of RNNCell.
inputs: A length T list of inputs, each a vector with shape [batch_size]. inputs: A length T list of inputs, each a tensor of shape
[batch_size, cell.input_size].
initial_state: (optional) An initial state for the RNN. This must be initial_state: (optional) An initial state for the RNN. This must be
a tensor of appropriate type and shape [batch_size x cell.state_size]. a tensor of appropriate type and shape [batch_size x cell.state_size].
dtype: (optional) The data type for the initial state. Required if dtype: (optional) The data type for the initial state. Required if
...@@ -124,7 +125,8 @@ def state_saving_rnn(cell, inputs, state_saver, state_name, ...@@ -124,7 +125,8 @@ def state_saving_rnn(cell, inputs, state_saver, state_name,
Args: Args:
cell: An instance of RNNCell. cell: An instance of RNNCell.
inputs: A length T list of inputs, each a vector with shape [batch_size]. inputs: A length T list of inputs, each a tensor of shape
[batch_size, cell.input_size].
state_saver: A state saver object with methods `state` and `save_state`. state_saver: A state saver object with methods `state` and `save_state`.
state_name: The name to use with the state_saver. state_name: The name to use with the state_saver.
sequence_length: (optional) An int64 vector (tensor) size [batch_size]. sequence_length: (optional) An int64 vector (tensor) size [batch_size].
...@@ -182,15 +184,17 @@ def bidirectional_rnn(cell_fw, cell_bw, inputs, ...@@ -182,15 +184,17 @@ def bidirectional_rnn(cell_fw, cell_bw, inputs,
Similar to the unidirectional case above (rnn) but takes input and builds Similar to the unidirectional case above (rnn) but takes input and builds
independent forward and backward RNNs with the final forward and backward independent forward and backward RNNs with the final forward and backward
outputs depth-concatenated, such that the output will have the format outputs depth-concatenated, such that the output will have the format
[time][batch][cell_fw.output_size + cell_bw.output_size]. The initial state [time][batch][cell_fw.output_size + cell_bw.output_size]. The input_size of
for both directions is zero by default (but can be set optionally) and no forward and backward cell must match. The initial state for both directions
intermediate states are ever returned -- the network is fully unrolled for is zero by default (but can be set optionally) and no intermediate states are
the given (passed in) length(s) of the sequence(s). ever returned -- the network is fully unrolled for the given (passed in)
length(s) of the sequence(s).
Args: Args:
cell_fw: An instance of RNNCell, to be used for forward direction. cell_fw: An instance of RNNCell, to be used for forward direction.
cell_bw: An instance of RNNCell, to be used for backward direction. cell_bw: An instance of RNNCell, to be used for backward direction.
inputs: A length T list of inputs, each a vector with shape [batch_size]. inputs: A length T list of inputs, each a tensor of shape
[batch_size, cell.input_size].
initial_state_fw: (optional) An initial state for the forward RNN. initial_state_fw: (optional) An initial state for the forward RNN.
This must be a tensor of appropriate type and shape This must be a tensor of appropriate type and shape
[batch_size x cell.state_size]. [batch_size x cell.state_size].
......
...@@ -198,6 +198,24 @@ class BasicLSTMCell(RNNCell): ...@@ -198,6 +198,24 @@ class BasicLSTMCell(RNNCell):
return new_h, array_ops.concat(1, [new_c, new_h]) return new_h, array_ops.concat(1, [new_c, new_h])
def _get_sharded_variable(name, shape, initializer, dtype, num_shards):
"""Get a list of sharded variables with the given dtype and initializer."""
unit_shard_size = int(math.ceil(shape[1] / num_shards))
shards = []
for i in range(num_shards):
current_size = min(unit_shard_size, shape[1] - unit_shard_size * i)
shards.append(vs.get_variable(name + "_%d" % i, [shape[0], current_size],
initializer=initializer, dtype=dtype))
return shards
def _matmul_with_sharded_variable(tensor, sharded_tensor):
"""Multiply tensor with each tensor in sharded_tensor and column-concat."""
return array_ops.concat(1, [math_ops.matmul(tensor, shard)
for shard in sharded_tensor])
class LSTMCell(RNNCell): class LSTMCell(RNNCell):
"""Long short-term memory unit (LSTM) recurrent network cell. """Long short-term memory unit (LSTM) recurrent network cell.
...@@ -231,15 +249,8 @@ class LSTMCell(RNNCell): ...@@ -231,15 +249,8 @@ class LSTMCell(RNNCell):
matrices. If None, no projection is performed. matrices. If None, no projection is performed.
num_unit_shards: How to split the weight matrix. If >1, the weight num_unit_shards: How to split the weight matrix. If >1, the weight
matrix is stored across num_unit_shards. matrix is stored across num_unit_shards.
Note that num_unit_shards must evenly divide num_units * 4.
num_proj_shards: How to split the projection matrix. If >1, the num_proj_shards: How to split the projection matrix. If >1, the
projection matrix is stored across num_proj_shards. projection matrix is stored across num_proj_shards.
Note that num_proj_shards must evenly divide num_proj
(if num_proj is not None).
Raises:
ValueError: if num_unit_shards doesn't divide 4 * num_units or
num_proj_shards doesn't divide num_proj
""" """
self._num_units = num_units self._num_units = num_units
self._input_size = input_size self._input_size = input_size
...@@ -250,11 +261,6 @@ class LSTMCell(RNNCell): ...@@ -250,11 +261,6 @@ class LSTMCell(RNNCell):
self._num_unit_shards = num_unit_shards self._num_unit_shards = num_unit_shards
self._num_proj_shards = num_proj_shards self._num_proj_shards = num_proj_shards
if (num_units * 4) % num_unit_shards != 0:
raise ValueError("num_unit_shards must evently divide 4 * num_units")
if num_proj and num_proj % num_proj_shards != 0:
raise ValueError("num_proj_shards must evently divide num_proj")
if num_proj: if num_proj:
self._state_size = num_units + num_proj self._state_size = num_units + num_proj
self._output_size = num_proj self._output_size = num_proj
...@@ -299,15 +305,10 @@ class LSTMCell(RNNCell): ...@@ -299,15 +305,10 @@ class LSTMCell(RNNCell):
dtype = input_.dtype dtype = input_.dtype
unit_shard_size = (4 * self._num_units) // self._num_unit_shards
with vs.variable_scope(scope or type(self).__name__): # "LSTMCell" with vs.variable_scope(scope or type(self).__name__): # "LSTMCell"
w = array_ops.concat( sharded_w = _get_sharded_variable(
1, "W", [self.input_size + num_proj, 4 * self._num_units],
[vs.get_variable("W_%d" % i, self._initializer, dtype, self._num_unit_shards)
shape=[self.input_size + num_proj, unit_shard_size],
initializer=self._initializer,
dtype=dtype) for i in xrange(self._num_unit_shards)])
b = vs.get_variable( b = vs.get_variable(
"B", shape=[4 * self._num_units], "B", shape=[4 * self._num_units],
...@@ -315,17 +316,24 @@ class LSTMCell(RNNCell): ...@@ -315,17 +316,24 @@ class LSTMCell(RNNCell):
# i = input_gate, j = new_input, f = forget_gate, o = output_gate # i = input_gate, j = new_input, f = forget_gate, o = output_gate
cell_inputs = array_ops.concat(1, [input_, m_prev]) cell_inputs = array_ops.concat(1, [input_, m_prev])
i, j, f, o = array_ops.split( lstm_matrix = nn_ops.bias_add(
1, 4, nn_ops.bias_add(math_ops.matmul(cell_inputs, w), b)) _matmul_with_sharded_variable(cell_inputs, sharded_w), b)
i, j, f, o = array_ops.split(1, 4, lstm_matrix)
# Diagonal connections # Diagonal connections
if self._use_peepholes: if self._use_peepholes:
w_f_diag = vs.get_variable( w_f_diag = vs.get_variable(
"W_F_diag", shape=[self._num_units], dtype=dtype) "W_F_diag", shape=[self._num_units],
initializer=self._initializer,
dtype=dtype)
w_i_diag = vs.get_variable( w_i_diag = vs.get_variable(
"W_I_diag", shape=[self._num_units], dtype=dtype) "W_I_diag", shape=[self._num_units],
initializer=self._initializer,
dtype=dtype)
w_o_diag = vs.get_variable( w_o_diag = vs.get_variable(
"W_O_diag", shape=[self._num_units], dtype=dtype) "W_O_diag", shape=[self._num_units],
initializer=self._initializer,
dtype=dtype)
if self._use_peepholes: if self._use_peepholes:
c = (sigmoid(f + 1 + w_f_diag * c_prev) * c_prev + c = (sigmoid(f + 1 + w_f_diag * c_prev) * c_prev +
...@@ -342,16 +350,11 @@ class LSTMCell(RNNCell): ...@@ -342,16 +350,11 @@ class LSTMCell(RNNCell):
m = sigmoid(o) * tanh(c) m = sigmoid(o) * tanh(c)
if self._num_proj is not None: if self._num_proj is not None:
proj_shard_size = self._num_proj // self._num_proj_shards sharded_w_proj = _get_sharded_variable(
w_proj = array_ops.concat( "W_P", [self._num_units, self._num_proj], self._initializer,
1, dtype, self._num_proj_shards)
[vs.get_variable("W_P_%d" % i,
shape=[self._num_units, proj_shard_size], m = _matmul_with_sharded_variable(m, sharded_w_proj)
initializer=self._initializer,
dtype=dtype)
for i in xrange(self._num_proj_shards)])
# TODO(ebrevdo), use matmulsum
m = math_ops.matmul(m, w_proj)
return m, array_ops.concat(1, [c, m]) return m, array_ops.concat(1, [c, m])
......
...@@ -174,7 +174,10 @@ class EventAccumulator(object): ...@@ -174,7 +174,10 @@ class EventAccumulator(object):
with self._generator_mutex: with self._generator_mutex:
for event in self._generator.Load(): for event in self._generator.Load():
## Check if the event happened after a crash ## Check if the event happened after a crash
if event.step < self.most_recent_step: ## file_version events always have step 0, ignore.
## TODO(danmane): Have this check for restart events explicitly
if (event.step < self.most_recent_step and
not event.HasField('file_version')):
## Keep data in reservoirs that has a step less than event.step ## Keep data in reservoirs that has a step less than event.step
_NotExpired = lambda x: x.step < event.step _NotExpired = lambda x: x.step < event.step
......
...@@ -391,6 +391,16 @@ class MockingEventAccumulatorTest(EventAccumulatorTest): ...@@ -391,6 +391,16 @@ class MockingEventAccumulatorTest(EventAccumulatorTest):
## Check that we have discarded 200 and 300 ## Check that we have discarded 200 and 300
self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 101, 201, 301]) self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 101, 201, 301])
def testFileVersionEventDoesntTriggerDiscard(self):
"""Test that file version event doesnt trigger data purge."""
gen = _EventGenerator()
acc = ea.EventAccumulator(gen)
gen.AddScalar('s1', wall_time=1, step=100, value=20)
ev = tf.Event(wall_time=2, step=0, file_version='0')
gen.AddEvent(ev)
acc.Reload()
self.assertEqual([x.step for x in acc.Scalars('s1')], [100])
class RealisticEventAccumulatorTest(EventAccumulatorTest): class RealisticEventAccumulatorTest(EventAccumulatorTest):
......
--- 2 --- --- 2 ---
Begin tracking TensorBoard changes. Begin tracking TensorBoard changes.
--- 3 ---
Change default # of scalar values to 1000
Fix bug where TensorBoard discards all values after a restart.
\ No newline at end of file
...@@ -55,8 +55,9 @@ flags.DEFINE_boolean('debug', False, 'Whether to run the app in debug mode. ' ...@@ -55,8 +55,9 @@ flags.DEFINE_boolean('debug', False, 'Whether to run the app in debug mode. '
'This increases log verbosity to DEBUG.') 'This increases log verbosity to DEBUG.')
flags.DEFINE_string('host', '127.0.0.1', 'What host to listen to. Defaults to ' flags.DEFINE_string('host', '0.0.0.0', 'What host to listen to. Defaults to '
'serving on localhost, set to 0.0.0.0 for remote access.') 'serving on 0.0.0.0, set to 127.0.0.1 (localhost) to'
'disable remote access (also quiets security warnings).')
flags.DEFINE_integer('port', 6006, 'What port to serve TensorBoard on.') flags.DEFINE_integer('port', 6006, 'What port to serve TensorBoard on.')
...@@ -66,7 +67,7 @@ FLAGS = flags.FLAGS ...@@ -66,7 +67,7 @@ FLAGS = flags.FLAGS
TENSORBOARD_SIZE_GUIDANCE = { TENSORBOARD_SIZE_GUIDANCE = {
event_accumulator.COMPRESSED_HISTOGRAMS: 500, event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4, event_accumulator.IMAGES: 4,
event_accumulator.SCALARS: 10000, event_accumulator.SCALARS: 1000,
event_accumulator.HISTOGRAMS: 1, event_accumulator.HISTOGRAMS: 1,
} }
......
...@@ -19,6 +19,7 @@ sh_binary( ...@@ -19,6 +19,7 @@ sh_binary(
"setup.py", "setup.py",
":simple_console", ":simple_console",
"//tensorflow:tensorflow_py", "//tensorflow:tensorflow_py",
"//tensorflow/examples/tutorials/mnist:package",
"//tensorflow/models/embedding:package", "//tensorflow/models/embedding:package",
"//tensorflow/models/image/cifar10:all_files", "//tensorflow/models/image/cifar10:all_files",
"//tensorflow/models/image/mnist:convolutional", "//tensorflow/models/image/mnist:convolutional",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册