| 265 | |
| 266 | # generate dataset pipline |
| 267 | def build_model_input(filename, batch_size, num_epochs): |
| 268 | def parse_csv(value): |
| 269 | tf.logging.info('Parsing {}'.format(filename)) |
| 270 | string_defaults = [[' '] for i in range(1, 19)] |
| 271 | label_defaults = [[0], [0]] |
| 272 | column_headers = INPUT_COLUMN |
| 273 | record_defaults = label_defaults + string_defaults |
| 274 | columns = tf.io.decode_csv(value, record_defaults=record_defaults) |
| 275 | all_columns = collections.OrderedDict(zip(column_headers, columns)) |
| 276 | labels = all_columns.pop(LABEL_COLUMN[0]) |
| 277 | all_columns.pop(BUY_COLUMN[0]) |
| 278 | features = all_columns |
| 279 | return features, labels |
| 280 | |
| 281 | def parse_parquet(value): |
| 282 | tf.logging.info('Parsing {}'.format(filename)) |
| 283 | labels = value.pop(LABEL_COLUMN[0]) |
| 284 | features = value |
| 285 | return features, labels |
| 286 | |
| 287 | '''Work Queue Feature''' |
| 288 | if args.workqueue and not args.tf: |
| 289 | from tensorflow.python.ops.work_queue import WorkQueue |
| 290 | work_queue = WorkQueue([filename], num_epochs=num_epochs) |
| 291 | # For multiple files: |
| 292 | # work_queue = WorkQueue([filename, filename1,filename2,filename3]) |
| 293 | files = work_queue.input_dataset() |
| 294 | else: |
| 295 | files = filename |
| 296 | # Extract lines from input files using the Dataset API. |
| 297 | if args.parquet_dataset and not args.tf: |
| 298 | from tensorflow.python.data.experimental.ops import parquet_dataset_ops |
| 299 | dataset = parquet_dataset_ops.ParquetDataset(files, |
| 300 | fields = PARQUET_INPUT_COLUMN, |
| 301 | batch_size=batch_size) |
| 302 | if args.parquet_dataset_shuffle: |
| 303 | dataset = dataset.shuffle(buffer_size=20000, |
| 304 | seed=args.seed) # fix seed for reproducing |
| 305 | if not args.workqueue: |
| 306 | dataset = dataset.repeat(num_epochs) |
| 307 | dataset = dataset.map(parse_parquet, num_parallel_calls=28) |
| 308 | else: |
| 309 | dataset = tf.data.TextLineDataset(files) |
| 310 | dataset = dataset.shuffle(buffer_size=20000, |
| 311 | seed=args.seed) # fix seed for reproducing |
| 312 | if not args.workqueue: |
| 313 | dataset = dataset.repeat(num_epochs) |
| 314 | dataset = dataset.batch(batch_size) |
| 315 | dataset = dataset.map(parse_csv, num_parallel_calls=28) |
| 316 | dataset = dataset.prefetch(2) |
| 317 | return dataset |
| 318 | |
| 319 | |
| 320 | # generate feature columns |